Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use AND and OR statement inside the component while comparing in react component

I’m unable to find proper syntax of the use of AND and OR using inside the component fragments.
Lets say i have data which may return condition in strings

conditionOne
conditionTwo
conditionThree

I can use if statment like this

<React.Fragment>
  {data.condition === 'conditionOne' ? <>Do This </> : <> Do Something Else </>} 
<React.Fragment/>

But I want to find out the similar syntax to find, if all conditions meets Or one of the condition meets than Do Something Else or Do This .

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can do

['conditionOne', 'conditionTwo', 'conditionThree'].includes(data.condition)

this would replace the data.condition === 'conditionOne' expression.

You can also store that array somewhere else or use a Set.

A less maintainable approach would be to use the logical operators, but that can cause noise in the conditional ternary operation.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading