0

So I know that if I only have one condition to check I can put { condition ? results : elseresults } in JSX instead of the

if (condition) {
  results
} else {
  elseresults
}

in normal Javascript. But is there a way to check multiple conditions, where I would do

if (condition) {
} else if (condition 2) {
} else {
}

in normal JavaScript?

2 Answers2

3

{ condition1 ? result1 : condition2 ? result2 : resultElse }

Same as regular js: https://stackoverflow.com/a/55958241/12270342

Jon Baltz
  • 202
  • 1
  • 7
1

You can do:

<>
    {condition && (jsx)}
    {condition2 && (jsx)}
</>

Other thing you can do is:

let jsx;
if (condition) {
    jsx = x
} else if (condition 2) {
    jsx = y
} else {
    jsx = z
}

<>{jsx}</>

Or you can do nested ternaries!

Alvin Abia
  • 1,221
  • 1
  • 17
  • 18