Search code examples
javascriptreactjsreduxjsxcreate-react-app

REACT JSX - Chained conditons


The following code works for me and gives me the expected results but I have doubts if I am doing it right.

{
  Conditon1?<ChildComponent />:Condition2?<p>Hi</p>:<p>Bye</p>
}

My concern is the chained conditional statement - is this valid?


Solution

  • Yes the code statement you wrote is perfectly fine and would work, but I prefer to use better indentation while writing nested conditions so that the other person who reads the code get better understanding. Moreover when I have more chained conditions, I prefer using if/else rather than ternary operators.