React Native 标签中的条件运算符

Sar*_*rah 0 javascript conditional-operator react-native

在这里反应原生初学者。我有一个按钮,其中的文本将根据条件进行更改。

所以如果"this.props.X ==='a'"按钮文本将是'start',
如果this.props.X ==='b'按钮文本将是'stop'。我怎样才能做到这一点?我可以在按钮标签内使用条件运算符吗?

我正在尝试这样但不起作用。

<Button 
  type = "primary"
  (this.props.X === 'a'? title = {'Start'} : title = {'Stop'})
 > 
</Button>
Run Code Online (Sandbox Code Playgroud)

Fel*_*ing 5

您必须将条件检查放在 prop 的值中:

title={this.props.X === 'a' ? 'Start' : 'Stop'}
Run Code Online (Sandbox Code Playgroud)

prop 的值可以是静态值,也可以是表达式的结果,如果将该表达式放在大括号 ( {...}) 中。