日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
React條件渲染

條件渲染可以將一個數(shù)組內(nèi)的所有數(shù)據(jù)依次展示在界面上,常用場景:文字列表、商品列表。React 中的條件渲染就和在 JavaScript 中的條件語句一樣。使用 JavaScript 操作符如 if 或者 條件操作符 來創(chuàng)建渲染當(dāng)前狀態(tài)的元素,并且讓 React 更新匹配的 UI

先來看兩個組件:

function UserGreeting(props) {
 return 

歡迎回來!

; } function GuestGreeting(props) {  return 

請先注冊。

; }

我們將創(chuàng)建一個 Greeting 組件,它會根據(jù)用戶是否登錄來顯示其中之一:

React 實例

function Greeting(props) {
 const isLoggedIn = props.isLoggedIn;
 if (isLoggedIn) {
   return 
  ;
 }
 return 
  ;
}

ReactDOM.render(
 // 嘗試修改 isLoggedIn={true}:
 
  
   false} />,  document.getElementById(
   'example') ); 
  

元素變量

你可以使用變量來儲存元素。它可以幫助你有條件的渲染組件的一部分,而輸出的其他部分不會更改。 在下面的例子中,我們將要創(chuàng)建一個名為 LoginControl 的有狀態(tài)的組件。 它會根據(jù)當(dāng)前的狀態(tài)來渲染 或 ,它也將渲染前面例子中的 。

React 實例

class LoginControl extends React.Component {
 constructor(props) {
   super(props);
   this.handleLoginClick = this.handleLoginClick.bind(this);
   this.handleLogoutClick = this.handleLogoutClick.bind(this);
   this.state = {isLoggedIn: false};
 }

 handleLoginClick() {
   this.setState({isLoggedIn: true});
 }

 handleLogoutClick() {
   this.setState({isLoggedIn: false});
 }

 render() {
   const isLoggedIn = this.state.isLoggedIn;

   let button = null;
   if (isLoggedIn) {
     button = 
  ;
   } else {
     button = 
  ;
   }

   return (
     
  
           
           {button}      
  
   );
 }
}

ReactDOM.render(
 
  ,
 document.getElementById('example')
);

與運算符 &&

你可以通過用花括號包裹代碼在 JSX 中嵌入任何表達(dá)式 ,也包括 JavaScript 的邏輯與 &&,它可以方便地條件渲染一個元素。

React 實例

function Mailbox(props) {
 const unreadMessages = props.unreadMessages;
 return (
   
  
         
   

Hello!

     {unreadMessages.length > 0 &&        

         您有 {unreadMessages.length} 條未讀信息。        

     }      ); } const messages = ['React', 'Re: React', 'Re:Re: React']; ReactDOM.render(   ,  document.getElementById('example') );

在 JavaScript 中,true && expression 總是返回 expression,而 false && expression 總是返回 false。 因此,如果條件是 true,&& 右側(cè)的元素就會被渲染,如果是 false,React 會忽略并跳過它。

三目運算符

條件渲染的另一種方法是使用 JavaScript 的條件運算符:

condition ? true : false。

在下面的例子中,我們用它來有條件的渲染一小段文本。

render() { const isLoggedIn = this.state.isLoggedIn; return (
The user is {isLoggedIn ? 'currently' : 'not'} logged in.
); }

同樣它也可以用在較大的表達(dá)式中,雖然不太直觀:

render() {
 const isLoggedIn = this.state.isLoggedIn;
 return (
   
  
         {isLoggedIn ? (        
         ) : (        
         )}    
  
 );
}

阻止組件渲染

在極少數(shù)情況下,你可能希望隱藏組件,即使它被其他組件渲染。讓 render 方法返回 null 而不是它的渲染結(jié)果即可實現(xiàn)。 在下面的例子中, 根據(jù)屬性 warn 的值條件渲染。如果 warn 的值是 false,則組件不會渲染:

React 實例

function WarningBanner(props) {
 if (!props.warn) {
   return null;
 }

 return (
   
  
   "warning">      警告!    
  
 );
}

class Page extends React.Component {
 constructor(props) {
   super(props);
   this.state = {showWarning: true}
   this.handleToggleClick = this.handleToggleClick.bind(this);
 }

 handleToggleClick() {
   this.setState(prevState => ({
     showWarning: !prevState.showWarning
   }));
 }

 render() {
   return (
     
  
           
           
             {this.state.showWarning ? '隱藏' : '顯示'}              
  
   );
 }
}

ReactDOM.render(
 
  ,
 document.getElementById('example')
);

組件的 render 方法返回 null 并不會影響該組件生命周期方法的回調(diào)。例如,componentWillUpdate 和 componentDidUpdate 依然可以被調(diào)用。


分享文章:React條件渲染
網(wǎng)址分享:http://m.5511xx.com/article/cdogdhd.html