1

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>
1

2 Answers 2

2

Try this:

{
  ( this.state.input === '' )  // ternary operator for checking condition
  ?
  this.props.value  // if condition satisfy
  :
  this.state.input  // if condition doesn't satisfy
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.