[英]How can I delete an item from my todo list in typescript
I am new to TS and I'm trying to delete one item out of the to-do list, but I am stuck on how to implement that.我是 TS 的新手,我正在尝试从待办事项列表中删除一项,但我一直坚持如何实施。 I need some help.
我需要一些帮助。 I created a deleteHandeler function that should go into the button underneath the Thank you in advance.
我创建了一个deleteHandeler function 应该把go 放到按钮下面 谢谢你了。
here is the todo list component.这是待办事项列表组件。 I have another form component that handles the input and the submit button, so nothing much going on there.
我有另一个处理输入和提交按钮的表单组件,所以那里没什么大不了的。
import { ToDoListItems } from "./Todo-list-item";
import "./index.css"
interface TodoListProps {
todos: Array<Todo>;
toggleTodo: (SelectedItems: Todo) => void;
}
const deleteHandler = (e: ChangeEvent<HTMLButtonElement>) => {
}
export const TodoList: React.FC<TodoListProps> = ({ todos, toggleTodo }) => {
return todos.length > 1 ? ( //if the value is greater than 3 then, the msg below will appear
(
<ul>
{todos.map((todo) => (
<li className="parent">
<ToDoListItems todo={todo} toggleTodo={toggleTodo}> </ToDoListItems>
<button className="DeleteTodo" onClick={() =>{console.log("clicked")}} >X</button>
</li>
))}
</ul>
)
) : (
<h2>Please add some Todos.</h2>
);
}
const initialTodos: Array<Todo> = [
{ text: "walk the dog!", complete: false },
{ text: " learn how to creat app with typescript", complete: false }
];
function App() {
const [todos, setTodos] = useState(initialTodos);
const toggleTodo: ToggleTodo = selectedTodo => {
const newTodos = todos.map(todoItems => {
if (todoItems === selectedTodo) {
return {
...todoItems,
complete: !todoItems.complete
};
}
return todoItems;
});
setTodos(newTodos);
};
const addTodo: AddTodo = newTodo => {
newTodo.trim() !== "" && setTodos([...todos, { text: newTodo, complete: false }])
}
return (
<React.Fragment>
<TodoList todos={todos} toggleTodo={toggleTodo} />
<AddTodoForm addTodo={addTodo} />
</React.Fragment>
);
}
export default App;
You can use .filter
to take out the related todo item you do want.您可以使用
.filter
取出您想要的相关待办事项。 Below example assume that you are using name
as a criterion.下面的示例假设您使用
name
作为标准。
export const TodoList: React.FC<TodoListProps> = ({ todos, toggleTodo, setNewToDo }) => {
const deleteHandler = = (e: ChangeEvent<HTMLButtonElement>) => {
const newToDoItems = todos.filter(todo => todo.name === e.target.name)
setNewToDo(newToDoItems)
}
return todos.length > 1 ? (
(
//...other code
<button className="DeleteTodo" onClick={deleteHandler} >X</button>
//...other code
)
}
Example例子
const deleteHandler = (id: any) => {
// Example you have to put these lines, where your state is sitting
// I assumed todo.id here as a unique identifier for your todos list
// this.setState({ todos: todos.filter((todo:any) => todo.id !== id ) });
}
export const TodoList: React.FC<TodoListProps> = ({ todos, toggleTodo }) => {
return todos.length > 1 ? ( //if the value is greater than 3 then, the msg below will appear
(
<ul>
{todos.map((todo) => (
<li className="parent">
<ToDoListItems todo={todo} toggleTodo={toggleTodo}> </ToDoListItems>
<button className="DeleteTodo" onClick={this.deleteHandler.bind(this, todo.id} >X</button>
</li>
))}
</ul>
) ) : (
<h2>Please add some Todos.</h2> ); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.