Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do multiple if else statements in React?

So I am trying to create an if else statement similar too the following in React...

(where aNumber is any number)

if(aNumber < 5){
    show a picture
}else if (aNumber < 10){
    show a different picture
}else if (aNumber < 15){
    show another different picture
}else{
    show a some other 4th picture
}

In my table this.state.sWA1 returns a wind speed, so If its above a certain value I want to show a different picture depending on how high the wind speed is

<td>{this.state.sWA1 > '15' ? <img src={require('../arrowUp.png')} /> : <img src={require('../arrowDown.png')} /> }</td>

This currently works but I can only seem to find help with one if else statement rather than several if else statements, can anyone help or suggest an alternative approach?

Thank you!

like image 986
user3115941 Avatar asked Dec 19 '25 00:12

user3115941


1 Answers

It's called conditional rendering

<div>
  { this.state.sWA1>15 && 
  <div>
    //show picture
  </div>}
  { this.state.sWA1>5 && this.state.sWA1<15
  <div>
    //show picture
  </div>}
</div>
like image 185
DroidNoob Avatar answered Dec 20 '25 16:12

DroidNoob



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!