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

136
Visualizações
add active class On react with out state and toggle using JS dom

I'm trying to add Active Class in React using JS dom, but it doesn't work for me Please help in finding the solution. Please give a complete solution that includes the code with a little explanation because I feel that I am confused in dealing with the Dom

<FilterOrder>
   <div className=""  onClick={ActiveClass}> Today      </div>
   <div className=""  onClick={ActiveClass}> Yesterday  </div>
   <div className=""  onClick={ActiveClass}> This Week  </div>

My Function =>

const ActiveClass=(e)=>{
const active=e.target.classList;
const prevActive=document.getElementsByClassName("Active");
prevActive.forEach((e)=>console.log("hello"));
active.classList.add("Active");
}

error =>

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

DOM mutation is very anti-pattern in React. Use React refs to get access to underlying DOMNodes, or just toggle the classnames the React way using some local state.

This said, the error is caused by getElementsByClassName returning some collection that isn't an actual Array, it returns a HTMLCollection, which is an array-like structure.

There is no forEach method to invoke. You can use Array.from if you want/need to iterate the returned elements.

const ActiveClass = (e) => {
  const active = e.target.classList;
  const prevActive = document.getElementsByClassName("Active");

  Array.from(prevActive).forEach((e) => console.log("hello"));

  active.classList.add("Active");
}
about 4 years ago · Juan Pablo Isaza Relatório
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