【发布时间】:2018-08-17 20:48:26
【问题描述】:
我正在尝试检查道具是否为空并向反应组件添加条件。
代码如下:
class Mycomp extends Component {
render() {
return (
<div className="Mycomp">
{this.props.title}
if (this.props.title != '') {
console.log('Title is not empty');
}
</div>
);
}
}
问题是我收到了 Unexpected Token 错误。
我该如何解决这个问题?
【问题讨论】:
-
将
if条件移到return语句之前。 (并使用!==btw) -
你不能拥有
if statement in your JSX -
{ this.props.title || console.log("title is empty") }
标签: javascript reactjs