只是在練習一個簡單的待辦事項串列,我希望能夠單擊串列中的一個專案將其洗掉。我想我已經非常接近了,但無法弄清楚如何從單擊的 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;
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;
待辦事項.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;
任何幫助或指導將不勝感激!
uj5u.com熱心網友回復:
您需要傳遞要洗掉的元素的索引。在洗掉處理程式中按索引不等于傳遞索引的元素過濾
const deleteItem = (index) => {
setTodos(todos => todos.filter((item, i) => i !== index));
};
并且在映射中
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>
);
};
改用該id屬性可能會更好。
const deleteItem = (id) => {
setTodos(todos => todos.filter((item) => item.id !== id));
};
...
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>
);
};
并且為了避免子組件中的匿名回呼,宣告handleDelete為柯里化函式。
const deleteItem = (id) => () => {
setTodos(todos => todos.filter((item) => item.id !== id));
};
...
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>
);
};
uj5u.com熱心網友回復:
首先,您需要將您的待辦事項 id 傳遞給 handle delete 然后您可以在那里訪問 id
{ props.items.map((todo, i) => (
<Todo
index={i}
key={todo.id}
title={todo.title}
date={todo.date}
handleDelete={()=> props.handleDelete(todo.id)}
/>
))}
您在此處訪問 ID
const deleteItem = (todoId) => {
const newTodos = todos.filter((item) => item.id !== todoId;
setTodos(newTodos);
};
uj5u.com熱心網友回復:
data-index在 Todo.js 中添加屬性
<li data-index={props.index} onClick={props.handleDelete} className="todo-item">
<h2>{props.title}</h2>
<span>
{day}, {month}, {year}
</span>
</li>
并將其洗掉 deleteTodo
const deleteItem = (e) => {
const newTodos = todos.filter((item) => item.index !== e.currentTarget.dataset.index)
setTodos(newTodos);
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/336730.html
標籤:javascript 反应 列表 反应功能组件
