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

238
Visualizações
React: Selecting an element by id prints the wrong element
handleRemoveItem(id) {
        const newResList = this.state.resolutions;

        function del(_i) { 
            delete newResList[_i];
        }

        let filtered = [];
        for(let i = 0; i < newResList.length; i++) {
            if(newResList[i].k === id) {
                let el = document.getElementById(newResList[i].title + newResList[i].k);
                //el.classList.remove('animate__fadeInDown');
                //el.classList.add('animate__fadeOutLeft');
                console.log(el);
                del(i); // delete newResList[i]
            }
        }
        filtered = newResList.filter(function(el) {
            return el != null;
        });
        console.log(filtered);
        this.setState({resolutions: filtered});
    }

Hello! So, I am working on this list in react, when I add two elements to my list, and remove the first one added, it prints the other element that is left in the list. Is this normal? the 'id' is applied on creation of the list element and contains the title + unique ID applied to the object.

Not to say that removing the list element doesn't work, but I've commented out the adding/removing of classes in this code because its being applied to the wrong element!

Any help is greatly appreciated


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

0

when working with React there is a virtual DOM which handles all id's and function calls by the react logic.

If you want to, for example, select a element, there is no need to do it. Call a function directly from the element itself with onClick attribute and when other elements depend on this change, introduce state which you manipulate then.

More in the React Doc: https://reactjs.org/

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