Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
Eliminar fila de una tabla, deleteRow(id), remove() o use useState() en Reactjs

Tengo un componente con una tabla que se carga con los datos que envia la API REST, mi duda mas que nada, es si es bueno borrar una fila con remove() o deleteRow(index) entrando al DOM o tengo que manipular el DOM a través de un estado (useState) para una buena práctica o algo así.

Información adicional: me he acostumbrado a manipular el DOM directamente con javascript puro al crear páginas web estáticas, pero en React creo que tienes que usar accesorios, estado, ganchos para manipular los elementos del DOM o ¿me equivoco? Debe haber casos, supongo.

ATRAVESANDO DOM

 export default function App() { const data = ['Eli','Smith', 'Jhon'] const handleDelete = (index,e) => { //I can save an ID in <tr> through datasets (data-) //to get it by traversing the DOM when the user clicks and delete it //also in my database (API), that's Ok? e.target.parentNode.parentNode.parentNode.deleteRow(index) } const rows = data.map((item, index) => { return ( <tr key={index}> <td>{item}</td> <td><button onClick={e => handleDelete(index,e)}>Delete</button></td> </tr> ) }) return ( <div className="App"> <h1>Hello CodeSandbox</h1> <table> {rows} </table> </div> ); }

Está bien ?

¿O tengo que poner las filas en un estado (useState) y actualizarlo eliminando y volviendo a renderizar?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

La mejor práctica es administrar datos a través del estado, sugeriría no hacer la manipulación DOM manualmente

 export default function App() { const [data, setData] = useState(['Eli','Smith', 'Jhon']); const handleDelete = (index,e) => { setData(data.filter((v, i) => i !== index)); } const rows = data.map((item, index) => { return ( <tr key={index}> <td>{item}</td> <td><button onClick={e => handleDelete(index,e)}>Delete</button></td> </tr> ) }) return ( <div className="App"> <h1>Hello CodeSandbox</h1> <table> {rows} </table> </div> ); }
about 4 years ago · Juan Pablo Isaza Report

0

use hook useState para guardar sus datos y luego con handlerDelete elimine el elemento y configure los datos. React volverá a renderizar el componente (porque su estado ha cambiado) y obtendrá el resultado necesario:

https://codesandbox.io/s/cranky-surf-iykn7?file=/src/App.js

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!