我有以下简单的短路语句,应显示一个组件或不显示任何内容:
{profileTypesLoading && <GeneralLoader />}
如果该语句为假,则呈现a 0而不是什么。
0
我所做的console.log(profileTypesLoading)只是为了快速查看该profileTypesLoading属性的状态,它是预期的1或0。0应该为假…什么也不会渲染。对?
console.log(profileTypesLoading)
profileTypesLoading
知道为什么会这样吗?
因为你的条件是falsy,因此不会返回第二个参数(<GeneralLoader />),它会返回profileTypesLoading,这是一个数字,所以反应会使其因为阵营跳过渲染什么,是typeof boolean或undefined,并会呈现什么是typeof string或number:
<GeneralLoader />
typeof
boolean
undefined
string
number
为了安全起见,您可以使用三元表达式{condition ? <Component /> : null}或布尔值将条件转换为{!!condition && <Component />}
{condition ? <Component /> : null}
{!!condition && <Component />}