Answer by Phaelax z for multiple condition in ternary operator in jsx
I would not use ternary because it gets hard to read. Why not store the status and associated colors in an object then just reference that?const colors = {approved:"blue", rejected:"red"};<div...
View ArticleAnswer by romail ahmad for multiple condition in ternary operator in jsx
Multiple condition in ternary operator in JSX and JSstyle={{'backgroundColor': status === 'approved' ? 'blue' : status === 'cancel' ? 'red' : 'green'}}
View ArticleAnswer by MW UMESH for multiple condition in ternary operator in jsx
Inside render you can create an empty array variable. As shown below, you can apply nested styling. Also, you won't need a nested ternary operator.let styleValue = [];if(status === 'approved') {...
View ArticleAnswer by The Reason for multiple condition in ternary operator in jsx
There is another way how to do it with the a bit more readable & cleaner code style. We can replace the ternary operator with the object literal and use this instead of nesting ternary operators,...
View ArticleAnswer by Dom for multiple condition in ternary operator in jsx
To chain ternary operations you need to add another ternary operator to be returned when the conditions are not met, for example:a === true ? a : bIn place of b you would add a new ternary operator,...
View ArticleAnswer by M0nst3R for multiple condition in ternary operator in jsx
I would suggest using functions if your conditions get complicated, to not degrade your code readability.getBackgroundColor(status) { if (status === 'approved') { return 'blue'; } if (status ===...
View ArticleAnswer by Hemerson Carlin for multiple condition in ternary operator in jsx
I'd handle it separately as other types of status may appear in the future. const getBackgroundColor(status) { if (status === 'approved') { return 'blue' } else if (status === 'pending') { return...
View ArticleAnswer by Mayank Shukla for multiple condition in ternary operator in jsx
Using multiple ternary operators is not a good idea, better to use a function and put if-else conditions inside that and call that function from render. It helps you to make the render part clean and...
View ArticleAnswer by Paul Fitzgerald for multiple condition in ternary operator in jsx
You could do the following:<div style={{'backgroundColor': status === 'approved' ? 'blue' : status === 'pending' ? 'black' : 'red'}}></div>This means if status === 'approved' set the...
View Articlemultiple 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',...
View ArticleAnswer by Syed Ali Abbas for multiple condition in ternary operator in jsx
You can also do it by simple short-circuiting syntax.<div style={{backgroundColor: (status === 'approved'&& 'blue') || (status === 'pending'&& 'black') || 'red'}}></div>This...
View ArticleAnswer by Paul Fitzgerald for multiple condition in ternary operator in jsx
you could use a switch statement like below as an alternative to the ternary statement.getBackgroundColor(status) { switch (status) { case 'approved': return 'blue'; case 'pending': return 'red';...
View Article