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

149
Vistas
How to delete in axios obj in to do list?

How can I remove the selected object by ID, via axios. Already tried many options. Trying to figure out how to work with the API, don't be alarmed if the errors are too obvious.

My state:

 const [table, setTable] = useState([]);

useEffect(() => {
axios.get("http://localhost:3004/item").then(({ data }) => {
  setTable(data);
})
}, [])

Delete function

  const removeItem = (item, id) => {
 axios.delete('http://localhost:3004/item' + item.id).then(() => {
 const newLists = table.filter(item => item.id !== id);
 setTable(newLists);
})}

And onClick:

<Fragment>
  <th><img onClick={() => removeItem(item.id, item)} src={close} alt="close" /></th>
</Fragment>

My db.json

 {
"item": [
{
  "text": "Пошел в свой первый класс",
  "id": 0,
  "data": {
    "year": 2012,
    "day": 25,
    "month": 1
  }
},
{
  "text": "Поехал на чемпионат по бейсболу",
  "id": 1,
  "data": {
    "year": 2018,
    "day": 14,
    "month": 3
  }
},
{
  "text": "Поступил в институт",
  "id": 2,
  "data": {
    "year": 2007,
    "day": 12,
    "month": 4
  }
},
{
  "id": 3,
  "text": "Разобрался с аксиос",
  "data": {
    "year": "2022",
    "day": "17",
    "month": "05"
  }
}
]}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Since you have your data in the state (static/not provided by some request), you can just remove that element from your state data list when delete request is returned, such as this example:

const removeItem = (item, id) => {
   axios.delete('http://localhost:3004/item' + item.id).then(() => {
      let aux = table;
      aux.forEach((item, index) => {
         if(item.id == id){
            aux.splice(index, 1);
            return;
         }
      });
      setTable(aux);
})};

[]s

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