Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render an element based on condition in React Native

How do I render an element based on a condition in React Native?

This is how I tried:

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>
  );
}

But this error message appears:

Expected a component class, got [object Object]
like image 430
splunk Avatar asked Aug 30 '26 13:08

splunk


1 Answers

You have misplaced your }:

render() {  
  return (
      <Text>amount is: {this.state.amount}</Text>
      {this.state.amount >= 85 ? <button>FIRST</button> : <button>SECOND</button>}
      <Text>some text</Text>
  );
}
like image 88
Tholle Avatar answered Sep 02 '26 05:09

Tholle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!