Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

157
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda