Files
Randell Dawson 0a1eeea424 fix(guide) Replace invalid prism code block names (#35961)
* 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
2019-05-15 19:08:19 +02:00

890 B
Raw Blame History

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> 
  ) 
 }