簡體   English   中英

根據React Native中的條件渲染元素

[英]Render an element based on condition in React Native

如何根據React Native中的條件渲染元素?

這是我嘗試的方式:

render() {  
  return (
      <Text>amount is: {this.state.amount}</Text> // it correctly prints amount value
      </Button>
         {this.state.amount} >= 85 ? <button>FIRST</button> :   <button>SECOND</button>
      <Text>some text</Text>
  );
}

但是會出現此錯誤消息:

Expected a component class, got [object Object]

你放錯了地方}

render() {  
  return (
      <Text>amount is: {this.state.amount}</Text>
      {this.state.amount >= 85 ? <button>FIRST</button> : <button>SECOND</button>}
      <Text>some text</Text>
  );
}

請記住,您必須將所有內容都包裝在視圖中。

render() {  
  return (
    <View>
      <Text>amount is: {this.state.amount}</Text>
         {this.state.amount >= 85 ? 
           <button>FIRST</button> : <button>SECOND</button> 
         }
      <Text>some text</Text>
    </View>
  );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM