javascript - 在 React Native 中根据条件渲染元素

标签 javascript reactjs 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>
  );
}

关于javascript - 在 React Native 中根据条件渲染元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41826647/

相关文章:

php - 关于从数据库中删除记录的确认

javascript - 使用 browserify 要求扩展的正确方法

javascript - 在 React Native 中将 3 个图像排成一行

reactjs - 开 Jest : fake setInterval not working as expected

react-native - RNFetchBlob 需要主队列设置

javascript - 多项选择测验也有对/错选项

javascript - jQuery在克隆选择选项后获取数据id

javascript - 语义 UI 中的表主体没有表行导致 validateDomNesting 错误

android - 多个设备/模拟器 react native react native 启动错误

react-native - OkHttp certificatePinner 似乎不起作用