I’m looking to display some html in my React/Next.js web app based on conditional logic. I got the basics working but having issues showing the same html if multiple variable conditions are true. For example, the following code works fine.
JavaScript
x
8
1
{category === 'ford' &&
2
<div>Car</div>
3
}
4
5
{category === 'harley' &&
6
<div>Motorcycle</div>
7
}
8
I’m having issues showing multiple variables as true. The following code doesn’t work but show the logic I’m after.
JavaScript
1
6
1
{category === 'ford' || category === 'toyota' &&
2
<div>Car</div>
3
}
4
5
//this code doesn't work.
6
I realise a simple answer is to separate operators for each separate condition, however i’m trying to avoid duplicating the html <div>Car</div>
(as in my actual application contains large forms in this section).
Advertisement
Answer
You will need to wrap the OR-Condition in parentheses like so:
JavaScript
1
2
1
(category === 'ford' || category === 'toyota') && <div>Car</div>
2