Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple condition in ternary operator in jsx

<div style={{'backgroundColor': status === 'approved' ? 'blue' : 'black'}}>
</div>

black is the default color but what if I want to add the 3rd condition?

status can be 'approved', 'rejected', 'pending' or more.

like image 742
Jenny Mok Avatar asked Sep 25 '17 15:09

Jenny Mok


1 Answers

You could do the following:

<div style={{'backgroundColor': status === 'approved' ? 'blue' : status === 'pending' ? 'black' : 'red'}}>
</div>

This means if status === 'approved' set the background color as blue, if status === 'pending' set it as black, else set it as red.

like image 199
Paul Fitzgerald Avatar answered Oct 13 '22 22:10

Paul Fitzgerald