Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

324
Vistas
How to remove table row on click in reactjs?

This is an addon to my previous question; I am trying to delete a specific row when the button is clicked, however everytime the button is clicked it deletes the next table row instead of itself. Here is what it looks like with a video. The current code I am using to perform this action is:

const [shop, setShop] = useState([]);

const singleDelete = (d) => {
        const newArr = [...shop];
        const index = shop.findIndex((contact) => contact.id === d);
        newArr.splice(index, 1);
        setShop(newArr);
}

return (
        <div className="content">
         <Table>
            <tbody id="tbid">
                    
                      {(shop.map((s,i) => (
                        <tr key={`${s+i}`} id={i} value={`${i}Row`}>
                        <td>{i}</td>
                        <td>{profil[i]}</td>
                        <td>{s}</td>
                        <td></td>
                        <td>{acc[i]}</td>
                        <td>
                        <Button className="btn-round btn-icon" color="danger" size="sm" type="submit" onClick={singleDelete}>
                              <i className="icon-simple-delete"/>
                          </Button>
                        </td>
                        </tr>
                      )))}
                    </tbody>    
         </Table>
        <div>
)

I have tried using setShop(shop.filter((_, index) => index !== d)); But the results are still the same.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The problem is you don't pass the ID into the singleDelete function.

Change this:

onClick={singleDelete}

to this:

onClick={ () => singleDelete(s.id) }

And it would be nice to rewrite singleDelete function like @stoen recommended.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Maybe try using filter instead:

const singleDelete = (d) => {
    setShop(shop.filter((contact) => contact.id !== d));
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda