JSX中的条件渲染

2022-03-02 reactjs javascript jsx

如何使用JSX进行条件呈现?

例如,我这里有div,如果道具的值为NULL,我希望呈现文本"不知道",否则,如果道具的值不等于NULL,则呈现道具。

例如:

<div>{ return this.props.date === null ? 'NO IDEA' : this.props.date }</div>

我试过这个,但没有效果。你知道我这里做错了什么吗?

提前感谢!


解决方案

您只需使用基于this.pros.date的三元运算符即可,因此不需要在此处返回

<div> {this.props.date === null ? 'NO IDEA' : this.props.date }</div>

相关文章