Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices for react conditional rendering?

I have a div/section that should be displayed when a button is toggled

{
  this.state.viewButtonClicked ?  <Solution solution= {this.state.activeSolution}/> : <React.Fragment/>
} 

Is using a ternary operator appropriate for this?

And in this case, what's the best practice to not display something if a button is false?

Is React.Fragment okay?

Basically I want to return/render null if viewButtonClicked is false.

like image 471
Allen H. Avatar asked Apr 24 '26 10:04

Allen H.


1 Answers

yes it should be fine. Additionally you can just do this

  {
       this.state.viewButtonClicked && <Solution solution={this.state.activeSolution}/>
  }   
like image 107
Rogelio Avatar answered Apr 27 '26 00:04

Rogelio



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!