1

How do we check multiple conditions in reactjs.
There's an ? operator, but it only checks one condition.

I want to check the status of three buttons. If it is true, they are rendered otherwise not.

Example -> If button 1 is true , render button 1
If button 2 is false, dont render button 2
If button 3 is true, render button 3

0

2 Answers 2

3

You can do

<div>
  {yourCondition1 && <button>Button 1</bitton>}
  {yourCondition2 && <button>Button 2</bitton>}
  {yourCondition3 && <button>Button 3</bitton>}
</div>
Sign up to request clarification or add additional context in comments.

Comments

1
{
      isButton_1 ? <button type="submit"> Button 1 </button>  : ' ' ||
      isButton_2 ? <button type="submit"> Button 2 </button>  : ' ' ||
      isButton_3 ? <button type="submit"> Button 3 </button>  : ' '
}

2 Comments

Please use four spaces indent to format your answer as code :)
I prefer the chosen answer but this will work too; I would only change the return value to null instead of an empty string(or in this case a space) when it evaluates to false.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.