无法理解如何使用 handleDelete 删除特定的 TODO。

huangapple go评论78阅读模式
英文:

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:

&lt;DeleteButton onClick={() =&gt; handleDelete(taskName)}&gt;Delete&lt;/DeleteButton&gt;

Then in the function you would receive that value and can use it to filter the list of tasks:

const handleDelete = (taskName: string) =&gt; {
  setTask(task.filter(t =&gt; t.taskName !== taskName));
};

huangapple
  • 本文由 发表于 2023年7月7日 00:45:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76630966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定