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

134
Visualizações
How to dynamically change the state in useEffect

I have a useEffect function that finds an element by it's ID and then if the element has a certain class then it set's the state to be either true or false. However this only happens on the initial load so if I was to click on the button (which is within the FoodList component) that removes/adds the class, then nothing happens. What's the best way to make this dynamic?

const [filterExpaned, setFilterExpanded] = useState<boolean>();

useEffect(() => {
    const listPanelToggle = document.getElementById('list-panel-toggle');

    if (listPanelToggle?.classList.contains('list-panel-toggle--collapsed')) {
        setFilterExpanded(true);
        console.log('It does have the class');
    } else {
        setFilterExpanded(false);
        console.log('It does not have the class');
    }
}, []);

return (
  <>
    <FoodList /> // The id 'list-panel-toggle' is within this component
    </>
);
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