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

261
Visualizações
Filtering data in a list (delete button) isn't working?

So I'm doing a list in which you can add items. When you add them you have two options:

  1. Delete the whole list
  2. Delete a specific item.

But for some reason the "handeDelete" button is not working. Can somebody tell me what did I write wrong in the code?

The link to CodeSandbox is:

codesandbox

import { useState } from "react";
import uuid from "react-uuid";

export default function ItemList() {
const [items, setItems] = useState({ item: "" });
const [groceryList, setGroceryList] = useState([]);

function handleChange(value, type) {
setItems((prev) => {
  return { ...prev, [type]: value };
});
}

function handleSubmit(e) {
e.preventDefault();
const newItem = { ...items, id: uuid() };
setGroceryList([...groceryList, newItem]);
setItems({ item: "" });
}

function handleDelete(id) {
setGroceryList(groceryList.filter((items) => items.id !== id));
}

return (
<>
  <form autoComplete="off" onSubmit={handleSubmit}>
    <input
      type="text"
      name="item"
      id="item"
      value={items.item}
      onChange={(e) => handleChange(e.target.value, "item")}
    />
  </form>
  {groceryList.map((list) => {
    return (
      <div key={list.id}>
        <ul>
          <li> {list.item}</li>
        </ul>
        <button onClick={(id) => handleDelete()}>Delete</button>
      </div>
    );
  })}
  <button onClick={() => setGroceryList([])}>Clear</button>
  </>
 );
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your delete button definition is wrong:

<button onClick={() => handleDelete(list.id)}>Delete</button>

the parameter you are receiving from the click event is not the id. Since you are not working with the event args itselfy you can safely ignore it. The second mistake was, that you are not passing the id itself to your handleDelete function.

For learning purposes, humor yourself and print the event to the console, while developing:

<button onClick={(evt) => {
          console.log(evt)
          handleDelete(list.id)
        }}>
    Delete
</button>

This will show you, that the parameter, that you named id (and I renamend to evt), is in fact reacts Synthetic Event: https://reactjs.org/docs/events.html

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