英文:
Can't understand how to delete a specific TODO using handleDelete
问题
我不太明白如何从Windows提示符中获取一个 id 或 indexOf 以便在 handleDelete 函数中使用。
我是否需要将Windows提示符更改为实际的输入字段。只是觉得如果有办法找到特定的Windows提示符,我可以用更少的代码行来完成这个操作。
先行谢过!
英文:
I don't really understand how to get a id or indexOf from a windows prompt to use the handleDelete function.
Would I have to switch up the windowsprompt to a actual input field. Just feels like if there is a way to find a way to identify a specific windowsprompt I would be able to do this in a less amount of lines.
Thank you in advance!
import * as React from 'react';
import styled from 'styled-components';
const Container = styled.div``;
const Todo = styled.div`
display: flex;
align-items: center;
`;
const TaskTitle = styled.p`
margin-right: 8px;
`;
const DeleteButton = styled.button``;
interface ITodo {
taskName: string;
}
export default function App() {
const [task, setTask] = React.useState<ITodo[]>([]);
const handleDelete = () => {
};
const handleClick = () => {
const promptResult = window.prompt('Enter name of new task');
const newTask: ITodo = {
taskName: promptResult,
};
setTask([...task, newTask]);
};
return (
<Container>
<button onClick={handleClick}>+</button>
{task.map(({ taskName }) => {
return (
<Todo>
<TaskTitle>{taskName}</TaskTitle>
<DeleteButton onClick={handleDelete}>Delete</DeleteButton>
</Todo>
);
})}
</Container>
);
}
答案1
得分: 1
对于任何给定的“任务”,唯一可用的信息是taskName
。所以如果这是唯一标识任务的方式,将该值传递给删除处理程序:
<DeleteButton onClick={() => handleDelete(taskName)}>删除</DeleteButton>
然后在函数中,您将收到该值,并可以使用它来过滤任务列表:
const handleDelete = (taskName: string) => {
setTask(task.filter(t => t.taskName !== taskName));
};
英文:
For any given "task" the only information available is taskName
. So if that's how a task is uniquely identified, pass that value to the delete handler:
<DeleteButton onClick={() => handleDelete(taskName)}>Delete</DeleteButton>
Then in the function you would receive that value and can use it to filter the list of tasks:
const handleDelete = (taskName: string) => {
setTask(task.filter(t => t.taskName !== taskName));
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论