【发布时间】:2020-03-22 11:46:29
【问题描述】:
您好,我被这个问题困住了,我刚刚开始使用 react js。它是 HOME js 组件 单击按钮时,我想显示一些文本(当标志为真时),否则它不应该显示任何内容。
import React from 'react';
export default class Home extends React.Component{
constructor(props)
{
super(props);
this.state = {flag: false}
this.showMe = this.showMe.bind(this);
}
showMe()
{
this.state.flag ? ((<div> show this when flag is true </div>) : null)
}
render()
{
return(
<div>
<h1>Welcome to the Tornadoes Website {this.state.flag}</h1>
<button type="button" onClick={this.showMe}>Click me </button>
</div>);
}
}
控制台错误:
16:2: Parsing error: Unexpected token, expected ":"
14 | {
15 | this.state.flag ? ((<div> show this when flag is true</div>) : null)
> 16 | }
| ^
17 |
18 |
19 | render()
【问题讨论】:
-
你有不平衡的括号,这是错误告诉你的。它看起来像一个简单的错字。
-
错字修正后,您得到的答案只是解释了另一个热门问题if-else statement inside jsx: ReactJS
标签: javascript html reactjs frontend