【发布时间】:2021-04-21 08:16:08
【问题描述】:
我有一个关于检查功能组件中是否未定义道具以呈现一个屏幕或另一个屏幕的问题。
我的功能组件:
const InfoHeader = ({population, infected, recovered, deaths, active}) => {
console.log(population)
return(
<div className="infoHeader-wrapper">
///code to render the information passed into the props
</div>
)
}
export default InfoHeader
我知道第一次加载的道具是未定义的,因为它们只有在用户与我创建的地图上的一个部分交互时才会获得一个值。
我的问题是:
而不是做类似的事情
if(population !==undefined && infected !== undefined && ... )
有没有更好的方法来创建一个三元运算符来有条件地在功能组件的返回中呈现一个或另一个元素?比如:
return(
allProps !== undefined ?? renderX : renderY
)
非常感谢
【问题讨论】:
标签: javascript reactjs conditional-operator conditional-rendering