如何使用React删除列表项(函数组件)

cd1*_*313 3 javascript list reactjs react-functional-component

只是为了练习而制作一个简单的待办事项列表,我希望能够单击列表中的某个项目将其删除。我认为我已经非常接近了,但无法弄清楚如何从单击的 li 中获取某种数据以将其与我的数组进行比较。

应用程序.js

import { useState } from "react";
import "./App.css";
import Newtodo from "./NewTodo/NewTodo";
import TodoList from "./TodoList/TodoList";

let INITIAL_TODOS = [
  { id: "e1", title: "Set up meeting", date: new Date(2021, 0, 14), index: 0 },
  {
    id: "e2",
    title: "Doctor appointment",
    date: new Date(2021, 2, 14),
    index: 1,
  },
  { id: "e3", title: "Work on project", date: new Date(2021, 1, 22), index: 2 },
  { id: "e4", title: "Update resume", date: new Date(2021, 6, 14), index: 3 },
];

const App = () => {
  const [todos, setTodos] = useState(INITIAL_TODOS);

  const deleteItem = (e) => {
    const newTodos = todos.filter((item) => item.index !== 1 /*This works properly with a hardcoded value(1) but how can this be done dynamically as e doesn't seem to have anything useful within it (like e.target.value)*/);
    setTodos(newTodos);
  };

  return (
    <div className="App">
      <Newtodo />
      <TodoList items={todos} handleDelete={deleteItem} />
    </div>
  );
};

export default App;
Run Code Online (Sandbox Code Playgroud)

TodoList.js

import "./TodoList.css";
import Todo from "./Todo";

const TodoList = (props) => {
  return (
    <div className="todo-list">
      <ul>
        {props.items.map((todo, i) => (
          <Todo
            index={i}
            key={todo.id}
            title={todo.title}
            date={todo.date}
            handleDelete={props.handleDelete}
          />
        ))}
      </ul>
    </div>
  );
};

export default TodoList;
Run Code Online (Sandbox Code Playgroud)

Todo.js

import "./Todo.css";

const Todo = (props) => {
  const month = props.date.toLocaleString("en-US", { month: "long" });
  const day = props.date.toLocaleString("en-US", { day: "2-digit" });
  const year = props.date.getFullYear();

  return (
    <li onClick={props.handleDelete} className="todo-item">
      <h2>{props.title}</h2>
      <span>
        {day}, {month}, {year}
      </span>
    </li>
  );
};

export default Todo;
Run Code Online (Sandbox Code Playgroud)

任何帮助或指导将不胜感激!

Dre*_*ese 6

您需要传递要删除的元素的索引。在删除处理程序中按索引不等于传递索引的元素进行过滤

const deleteItem = (index) => {
  setTodos(todos => todos.filter((item, i) => i !== index));
};
Run Code Online (Sandbox Code Playgroud)

并且在映射中

const TodoList = (props) => {
  return (
    <div className="todo-list">
      <ul>
        {props.items.map((todo, i) => (
          <Todo
            index={i}
            key={todo.id}
            title={todo.title}
            date={todo.date}
            handleDelete={() => props.handleDelete(i)}
          />
        ))}
      </ul>
    </div>
  );
};
Run Code Online (Sandbox Code Playgroud)

使用该id属性可能会更好。

const deleteItem = (id) => {
  setTodos(todos => todos.filter((item) => item.id !== id));
};
Run Code Online (Sandbox Code Playgroud)

...

const TodoList = (props) => {
  return (
    <div className="todo-list">
      <ul>
        {props.items.map((todo, i) => (
          <Todo
            index={i}
            key={todo.id}
            title={todo.title}
            date={todo.date}
            handleDelete={() => props.handleDelete(todo.id)}
          />
        ))}
      </ul>
    </div>
  );
};
Run Code Online (Sandbox Code Playgroud)

为了避免子组件中的匿名回调,请声明handleDelete为柯里化函数。

const deleteItem = (id) => () => {
  setTodos(todos => todos.filter((item) => item.id !== id));
};
Run Code Online (Sandbox Code Playgroud)

...

const TodoList = (props) => {
  return (
    <div className="todo-list">
      <ul>
        {props.items.map((todo, i) => (
          <Todo
            index={i}
            key={todo.id}
            title={todo.title}
            date={todo.date}
            handleDelete={props.handleDelete(todo.id)}
          />
        ))}
      </ul>
    </div>
  );
};
Run Code Online (Sandbox Code Playgroud)