* fix: replace sh with shell fix replace terminal with shell fix replace node with js fix replace output with shell fix replace cs with csharp fix replace c++ with cpp fix replace c# with csharp fix replace javasctipt with js fix replace syntax with js fix replace unix with shell fix replace linux with shell fix replace java 8 with java fix replace swift4 with swift fix replace react.js with jsx fix replace javascriot with js fix replace javacsript with js fix replace c++ - with cpp fix: corrected various typos fix: replace Algorithm with nothing fix: replace xaml with xml fix: replace solidity with nothing fix: replace c++ with cpp fix: replace txt with shell fix: replace code with json and css fix: replace console with shell
890 B
890 B
title, localeTitle
title | localeTitle |
---|---|
Render with an If/Else Condition | 使用If / Else条件渲染 |
使用If / Else条件渲染
方法
在组件的render方法内部,编写if / else语句,每个语句都有自己的返回方法,该方法具有不同的JSX。这使程序员能够根据各种条件呈现不同的UI。
首先,将当前返回方法包装在if语句中,并设置条件以检查变量'display'是否为true。请记住,您使用this.state
访问状态。
解
if (this.state.display === true) {
return (
<div>
<button onClick={this.toggleDisplay}>Toggle Display</button>
<h1>Displayed!</h1>
</div>
);
}
接下来,创建一个else语句,返回不带 h1
元素的相同JSX。
else {
return (
<div>
<button onClick={this.toggleDisplay}>Toggle Display</button>
</div>
)
}