简体   繁体   中英

conditional display in react native

I am trying to render text in a text component that is conditional. if, this.state.input is an empty string, then it should display this.props.value . otherwise, it should display this.state.input is there a way to do that?

<text>{
code??
}</text>

Try this:

{
  ( this.state.input === '' )  // ternary operator for checking condition
  ?
  this.props.value  // if condition satisfy
  :
  this.state.input  // if condition doesn't satisfy
}

The cleanest way would be

<Text>{this.props.input || this.props.value}</Text>

Which means if this.props.input is not a falsy value(including an empty string), it will render that, otherwise, it will render this.props.value .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM