Javascript 如何删除React Hooks/Redux应用程序中的特定列表项?

Javascript 如何删除React Hooks/Redux应用程序中的特定列表项?,javascript,reactjs,react-hooks,Javascript,Reactjs,React Hooks,我得到了一个ListComponent,我希望从我的redux存储中显示所有待办事项 问题是,通过单击旁边的按钮,我如何知道要删除哪个特定列表项 我的ListComponent如下所示: const ListComponent=()=>{ const todoArray=useSelector(state=>state.todos); const dispatch=usedpatch(); const title=todoArray.length==0?您无事可做?:“您的待办事项列表:”; 常

我得到了一个ListComponent,我希望从我的redux存储中显示所有待办事项

问题是,通过单击旁边的按钮,我如何知道要删除哪个特定列表项

我的ListComponent如下所示:

const ListComponent=()=>{
const todoArray=useSelector(state=>state.todos);
const dispatch=usedpatch();
const title=todoArray.length==0?您无事可做?:“您的待办事项列表:”;
常量deleteItem=()=>{
//如何知道要删除的项目?
调度(删除TODO(项目))
}
报税表(
{title}
    {todoArray.map((项目,i)=>)
  • {item}{i}

    乌特沃佐诺21.11.2020 o 9:20
  • ))}
); }
单击删除项目时,需要传递id引用,以便在知道要删除的项目之后

为此,请在函数中添加id或任何要用于识别它的引用 像这样

//在.map中添加id引用作为参数
deleteItem(item.id)}className=“btn btn sm btn大纲危险”alt=“delete”>
//在你的职责范围内
常量deleteItem=(itemId)=>{
//如果你想在行动中筛选项目
调度(删除TODO(项目ID))
//如果要立即筛选并直接发送要删除的项目
const item=todoArray.find((item)=>item.id==itemId)
调度(删除TODO(项目))
}

todoArray中您的物品的有效载荷是多少?你有任何id吗?你必须传递一个唯一的标识符才能将要删除的项目作为目标。在这种情况下,您可以使用索引,但如果您的项目具有ID键,则更好。类似于onClick={()=>deleteItem(id)}“onClick={()=>deleteItem(id)}”Ye,这可以工作,因为我的负载现在只是一个字符串,thx。
   // in your .map add the id reference as parameter
   <button onClick={() => deleteItem(item.id)} className="btn btn-sm btn-outline-danger" alt="delete">


//In your function

  const deleteItem = (itemId) => {

    // if you gonna filter the item in your action  
    dispatch(deleteTodo(itemId))

    // if you want to filter now and directly send the item to delete
    const item = todoArray.find((item) => item.id === itemId)
    dispatch(deleteTodo(item))    
  }