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

406
Visualizações
How do I delete a specific row in a table in reactjs?

My last question was about deleting all rows in a table which is solved. The problem I am now having is how I would go abouts deleting a specific row which has a delete button on it. What I am trying to achieve is that when the button is clicked, that row which the button is on deleted.

The issue I am having now is that when I press the delete button on the row, it deletes the very first row on the table which is not what I want.

const [shop, setShop] = useState([]);
....
function singleDelete(i){
        var newArr = [...shop];
        newArr.splice(i, 1);
        setShop(newArr);
        setCount(count -1);
  }
      
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can use Array.prototype.filter:

setShop(shop.filter((_, index) => index !== i));

Also, don't maintain a separate count variable as it is redundant. The count can easily be retrieved with shop.length.

about 4 years ago · Juan Pablo Isaza Relatório

0

Using filter: setShop(shop.filter((_, shopIndex) = shopIndex !== i));

about 4 years ago · Juan Pablo Isaza Relatório

0

Created working example here, you can use this sandbox Link: https://codepen.io/naveenkumarpg/pen/GRvKpxM

for the reference you can take row id and delete the item.

remove = (rowId) => {
    // Array.prototype.filter returns new array
    // so we aren't mutating state here
    const arrayCopy = this.state.data.filter((row) => row.id !== rowId);
    this.setState({data: arrayCopy});
};
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