Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
How to simply nested ternary logic in react while rendering different component

I have the following logic inside a react component where, I am rendering different component based on the boolean values. This peace of code is very difficult to understand. Are there anyways, I can simply that logic:

      {isEnabled ? (
      <>
        {!loading ? (
          <>
            {items.length === 0 ? (
              <>
                <ComponentOne/>
                <Container>
                  <img src={Image} alt="Image" />
                </Container>
              </>
            ) : (
              <ComponentTwo/>
            )}
          </>
        ) : (
          <div>
            <LoadingComponent/>
          </div>
        )}
      </>
    ) : (
      <ComponentThree/>
    )}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I'd probably split it up into seperate components and pass parameters down the component tree for example

{isEnabled ? <IsLoadingComponent loading={loading} items={items}> : <ComponentThree/>}
about 4 years ago · Juan Pablo Isaza Report

0

You might find it useful to split the component up into a "Loading" version and a "Loaded" version so you don't have to handle both states in the same component. Then the component basically just renders the "Loading" or "Loaded" version depending on the flag.

But even without that, you can at least make that easier to debug by using if/else if etc. and assigning to a temporary variable:

let comp;
if (isEnabled) {
    if (loading) {
        comp = <div>
            <LoadingComponent/>
        </div>;
    } else if (items.length === 0) {
        comp = <>
            <ComponentOne/>
            <Container>
                <img src={Image} alt="Image" />
            </Container>
        </>;
    } else {
        comp = <ComponentTwo />;
    }
} else {
    comp = <ComponentThree />;
}

Then just

{comp}

where that nested conditional was.

about 4 years ago · Juan Pablo Isaza Report

0

I think you are making a simple thing very complicated. What we can do instead is that make use of "&&".

  { isEnabled && loading && <LoaderComponent /> }
   {isEnabled && !items.length  &&
     <>
     <ComponentOne/>
     <Container>
        <img src={Image} alt="Image" />
     </Container>
  </>
}
{isEnabled && items.length && <ComponentTwo/>}
{!isEnabled && <ComponentThree />}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!