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

156
Views
How to preserve state on specific conditional rendering?

I have a page on react, where I have so that when the user is on a desktop screen size, it shows both components together, but on smaller screens it shows only one with the ability to switch. Right now I'm using a custom hook that returns a boolean called desktop (true if desktop size, false if not). I'm also using a state variable to track the change between components.

{!desktop ?
   switch ?
         <>{List}</>
      :
         <>{Map}</>
   : 
   <>{Map}{List}</>
}

When switching between desktop/not, it resets the state on both components. I understand that this is what the above code is supposed to do, because you're rendering a new instance of the component.

My question is there a better way to achieve my desired outcome with a different conditional render? And if not is there a way to preserve the state when desktop does switch? Any help would be greatly appreciated!

about 4 years ago · Juan Pablo Isaza
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!