简体   繁体   English

我无法删除使用 react 创建的待办事项列表中的项目,请帮助我解决代码中的错误

[英]I am not able to delete items in my todo list made with react, please help me with error in my code

below is my code for todo list i am not able to delete items in my list in delete items function please help me with error.下面是我的待办事项列表代码,我无法在删除项目功能中删除列表中的项目,请帮助我解决错误。

import React, { useState } from "react";
import "./styles.css";

export default function App() {
  const [inputList, setInputList] = useState("");
  const [items, setItems] = useState([]);
  const change1 = (e) => {
    setInputList(e.target.value);
  };
  const change2 = () => {
    setItems((oldItems) => {
      return [...oldItems, inputList];
    });
    setInputList("");
  };
  const deleteItem = (ind) => {
    return setItems(items.filter((item)=>{return item.ind!==ind}))
  }
  return (
    <div className="App">
      <div className="inner_div">
        <h1 style={{ borderBottom: "2px solid black" }}>ToDo List</h1>
        <input type="text" onChange={change1} value={inputList} />
        &nbsp;&nbsp;
        <button onClick={change2}>+</button>
        <ol style={{ listStyle: "none" }}>
          {items.map((itemval, ind) => {
            return (
              <div style={{ display: "flex" }}>
                <button onClick={deleteItem}>-</button>&nbsp;
                <li id={ind}>{itemval}</li>
              </div>
            );
          })}
        </ol>
      </div>
    </div>
  );
}

Issue问题

You are passing the click event to the deleteItem handler.您正在将 click 事件传递给deleteItem处理程序。

const deleteItem = (ind) => { // <-- click event object!
  return setItems(items.filter((item)=>{return item.ind!==ind}))
}

...

<button onClick={deleteItem}>-</button>

There is also an issue with your filter function, you are comparing the index to a property on the items and this condition will almost always be false and clear the items array.您的过滤器功能也存在问题,您正在将索引与项目上的属性进行比较,并且此条件几乎总是为假并清除items数组。

(item)=>{return item.ind!==ind}) // item.ind is undefined

Solution解决方案

Fix the deleteItem handler to correctly compare indices.修复deleteItem处理程序以正确比较索引。

const deleteItem = (index) => {
  setItems(items => items.filter((item, i)=> i !== index));
}

You need to pass the mapping index value to the callback.您需要将映射索引值传递给回调。 Don't forget to add an unique React key to the mapped elements.不要忘记为映射元素添加唯一的 React 键。 Since you've no GUIDs on the item elements the array index will have to do for now.由于您在 item 元素上没有 GUID,因此数组索引现在必须完成。

{items.map((itemval, index) => {
  return (
    <div key={ind} style={{ display: "flex" }}>
      <button onClick={() => deleteItem(index)}>-</button>&nbsp;
      <li id={index}>{itemval}</li>
    </div>
  );
})}

If you want to avoid the anonymous function in the button's onClick handler you can convert deleteItem to a curried function and enclose/close over the specific todo's index value.如果您想避免按钮的onClick处理程序中的匿名函数,您可以将deleteItem转换为deleteItem函数并封闭/关闭特定 todo 的索引值。 A function is returned to serve as the onClick callback.返回一个函数作为onClick回调。

const deleteItem = (index) => () => {
  setItems(items => items.filter((item, i)=> i !== index));
}

...

<button onClick={deleteItem(index)}>-</button>

Try this!试试这个! This should work这应该工作

Items does not have any ind properties.项目没有任何 ind 属性。 Index is the second parameter of the filter and you must use it in the filter to remove, and you have to pass the index in the function deleteItem (ind).索引是过滤器的第二个参数,必须在过滤器中使用它才能删除,并且必须在函数deleteItem(ind)中传递索引。

 import React, { useState } from "react";
 import "./styles.css";

  export default function App() {
   const [inputList, setInputList] = useState("");
   const [items, setItems] = useState([]);
   const change1 = (e) => {
   setInputList(e.target.value);
 };
 const change2 = () => {
 setItems((oldItems) => {
  return [...oldItems, inputList];
 });
 setInputList("");
};
const deleteItem = (ind) => {
return setItems(
  items.filter((item, i) => {
    return i !== ind;
   })
  );
 };
  return (
   <div className="App">
   <div className="inner_div">
    <h1 style={{ borderBottom: "2px solid black" }}>ToDo List</h1>
    <input type="text" onChange={change1} value={inputList} />
    &nbsp;&nbsp;
    <button onClick={change2}>+</button>
    <ol style={{ listStyle: "none" }}>
      {items.map((itemval, ind) => {
        return (
          <div style={{ display: "flex" }}>
            <button onClick={() => deleteItem(ind)}>-</button>&nbsp;
            <li id={ind}>{itemval}</li>
          </div>
        );
      })}
    </ol>
  </div>
</div>
 );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在做一个反应项目,我的项目中有这个错误,它与谷歌地图有关,请帮我解决这个问题 - I am doing an react project and I have this error in my project which releted to google maps , please help me to solve this 当我编写 npm start 并收到此错误时,我的反应应用程序没有启动,任何人都可以帮助我,我是新来的反应。 我正在使用 Acode(Editor) 和 termux - My react app does not start when I write npm start and get this error can anyone please help me I am new to react. I am using Acode(Editor) and termux 我正在尝试显示来自外部 api 的数据,如果有人可以帮助我解决代码中的问题 - i am trying to display data from an external api, if someone can please help me to fix the issue in my code 请帮我找出为什么我的反应组件没有正确地在 jsx 中映射我的列表 - Please help me find out why my react component is not mapping my list in jsx correct 为什么 id 变量在我的待办事项列表删除组件的反应代码中未定义? - Why id variable is coming undefined in my react code in todo list Delete component? 无法使用 react 和 redux 在待办事项列表中添加待办事项。 我究竟做错了什么? - Not able to add todo in a todo-list using react and redux. What am I doing wrong? 如何使用钩子在 React 中编辑我的待办事项? - How can I edit my todo items in React using hooks? 你好,我想将一个项目从待办事项列表移动到 React js 中的完成列表,这是我的代码 - Hello i want to move an item from Todo list to done list in react js here is my code 我无法在我的 React 应用程序中减小由 google-maps-react 制作的 Google Maps 组件的大小 - I am not able to reduce the size of the Google Maps Component made by google-maps-react in my React application 为什么我在这里收到错误? 反应待办事项列表 - Why am I getting an error here? React Todo list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM