React showing 0 instead of nothing with short-circuit (&&) conditional component

后端 未结 3 1520
闹比i
闹比i 2020-12-13 05:42

I have the following simple short-circuit statement that should show either a component or nothing:

{profileTypesLoading && }

3条回答
  •  忘掉有多难
    2020-12-13 06:15

    Since your condition is falsy and so doesn't return the second argument (), it will return profileTypesLoading, which is a number, so react will render it because React skips rendering for anything that is typeof boolean or undefined and will render anything that is typeof string or number:

    To make it safe, you can either use a ternary expression {condition ? : null} or boolean cast your condition like {!!condition && }

提交回复
热议问题