kerim Erkan
kerim Erkan

Reputation: 171

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>

Upvotes: 1

Views: 1099

Answers (2)

Mertcan Seğmen
Mertcan Seğmen

Reputation: 933

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.

Upvotes: 0

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

Try this:

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

Upvotes: 2

Related Questions