1

Is it possible to write a conditional render with an OR operator in the statement? to avoid double code.

{regions && || regionsWithAnimals && (
  <>
    <h4>Title</h4>
    <div>
      <RegionsMap
        regions={regions || regionsWithAnimals.regions }
      />
    </div>
  </>
)}

Something like this, this is not working of course

EDIT: I could write:

{regions &&  (
  <>
    <h4>Title</h4>
    <div>
      <RegionsMap
        regions={regions }
      />
    </div>
  </>
)}

and below:

{regionsWithAnimals && (
  <>
    <h4>Title</h4>
    <div>
      <RegionsMap
        regions={regionsWithAnimals.regions }
      />
    </div>
  </>
)}

This is what I want to achieve, but I'm calling two times the same component.

5
  • What do you mean by "avoid double code"? Commented Apr 9, 2020 at 9:51
  • Do you mean ternary operator ? And please describe your problem with proper source code, its unclear. Commented Apr 9, 2020 at 9:53
  • I could write: ``` {regions && ( <> <h4>Title</h4> <div> <RegionsMap regions={regions } /> </div> </> )} ``` and below: ``` {regionsWithAnimals && ( <> <h4>Title</h4> <div> <RegionsMap regions={regionsWithAnimals } /> </div> </> )} ``` This is what I want to achieve, but I'm calling two times the same component. Commented Apr 9, 2020 at 9:54
  • 1
    (regions || regionsWithAnimals) && ... Though I should point out in your example if both are true they each render, but what you describe you want is for one or the other to render. Just keep the conditions in the same order in the component. Commented Apr 9, 2020 at 9:56
  • I'm sorry guys, the comment don't format. I updated the startpost with the code I want to achieve. @DrewReese If I do that, it will give me the error Type '{ name: string; path: string; }[] | undefined' is not assignable to type 'regions'. Type 'undefined' is not assignable to type '{ name: string; path: string; }[]'. By defining the props 'regions'. Commented Apr 9, 2020 at 9:58

1 Answer 1

5

Try something like this:

{(regions || regionsWithAnimals && regionsWithAnimals.regions) && (
  <>
    <h4>Title</h4>
    <div>
      <RegionsMap
        regions={regions || regionsWithAnimals.regions  }
       />
    </div>
  </>
)}
Sign up to request clarification or add additional context in comments.

7 Comments

Object is possibly undefined if do that
What "Object' ?
<RegionsMap regions={regions || regionsWithAnimals.regions } /> I'm sorry, I missed some code. regionsWithAnimals.regions is possibly undefined. And if I write the following code: regions={RegionsPerCountry || isRegion?.regions} it will give the following error: Type '{ name: string; path: string; }[] | undefined' is not assignable to type 'regions'. Type 'undefined' is not assignable to type '{ name: string; path: string; }[]'.
I updated the Answer, you check also for regionsWithAnimals && regionsWithAnimals.regions
Still object is possibly undefined after your updated answer
|

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.