简体   繁体   English

根据React Native中的条件渲染元素

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

How do I render an element based on a condition in React Native ? 如何根据React Native中的条件渲染元素?

This is how I tried: 这是我尝试的方式:

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

But this error message appears: 但是会出现此错误消息:

Expected a component class, got [object Object]

You have misplaced your } : 你放错了地方}

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

Keep in mind that you have to wrap everything inside a view. 请记住,您必须将所有内容都包装在视图中。

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