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

303
Views
How to rewrite the code using conditional rendering in react and javascript?

i have code like below

const mode = isActiveMode;

{!isActiveMode && runId && !isEmpty(items) && (
    <>
        <Flex>
            //more jsx similar
            <Icon onClick={() => 
                setItemState((previous: any) => ({
                    ...previous,
                    items: undefined,
                }))
            }
        />
        <TabPanel>
            <Table />
        </TabPanel>
    </>
)}

{isActiveMode && runId && !isEmpty(items) && isTableOpen && (
    <>
        <Flex>
            //more jsx similar
            <Icon onClick={() => 
                setItemState((previous: any) => ({
                    ...previous,
                    items: undefined,
                }))
            }
        />
        <TabPanel>
            {selections.length === 1 ? (
                <Table/> ): ( <div>hello</div>)
            }
        </TabPanel>
        
            
    </>
)}

i have two renderings based on different conditions. the code inside is almost same in both cases. how can i reuse the code in this case. could someone please help me with this.

How can i rewrite above code into one with conditions using reusable code. thanks.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

From what I can see your jsx is pretty similar how about that one condition which was different you put it in ternary operator because table was rendering

const mode = isActiveMode;

{!isActiveMode && runId && !isEmpty(items) && (
    <>
    <Flex>
        //more jsx similar
        <Icon onClick={() => 
            setItemState((previous: any) => ({
                ...previous,
                items: undefined,
            }))
        }
     />
     <TabPanel>
         {isTableOpen===false || selections.length === 1 ? (
             <Table/> ): ( <div>hello</div>)
         }
     </TabPanel>
    
        
    </>
)}

Tell me if this isn't what you were looking for I'll try something different then

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!