简体   繁体   English

React hooks - 编辑后更新列表中的项目

[英]React hooks - Update item in list after edit

I'm new to React Hooks.我是 React Hooks 的新手。 I am trying to make a small crud app, with add, edit, delete.我正在尝试制作一个带有添加、编辑、删除功能的小型 crud 应用程序。 I have some issues getting my edit function to work.我在让我的编辑功能正常工作时遇到了一些问题。 When I click the edit, the input will add the values of the element that is clicked.当我单击编辑时,输入将添加被单击元素的值。 But how do I update the item?但是如何更新项目?

const [items, setItems] = useState<any>([]);
const [comment, setComment] = useState("");
const [amount, setAmount] = useState("");

const handleAddSubmit = (e: any) => {
    e.preventDefault();

    const newItem = {
        id: new Date().getTime(),
        comment: comment,
        amount: amount,
        preview: preview
    }
    setItems([...items].concat(newItem));

    setFile(null);
    setComment("");
    setAmount("");
};

 const editItem = (item: any) => {
    setFile(undefined);
    setComment(item.comment);
    setAmount(item.amount);
};

const deleteItem = (id: any) => {
    const updatedItems = [...items].filter((item) => item.id !== id);

    setItems(updatedItems);
}

return (
       <div>
        {items.map((item: any, index: any) => {
                return (
                    <div key={item.id}>
                        <div>{item.comment}</div>
                        <div>{item.amount} $</div>
                        <div onClick={() => editItem(item)}>Edit</div>
                        <div onClick={(e) => deleteItem(item.id)}>Delete</div>
                    </div>
                );
            })}

               <input
                value={comment}
                onChange={(e) => setComment(e.target.value)}
                placeholder="Comment"
            />
            <input
                value={amount}
                onChange={(e) => setAmount(e.target.value)}
                type="number"
            />
            <button formAction="submit">Add</button>
        </div>

) )

You can use a map to go through each element of a list and create a new object for the item you want to update:您可以使用地图遍历列表的每个元素并为要更新的项目创建一个新对象:

const editItem = (item) => {
  setItems(items.map(i => i.id === item.id
    ? {...i, comment, amount}
    : i);
  setComment("");
  setAmount("");
}

So acc to your code when you click on the edit button, this function gets triggered所以当你点击编辑按钮时,根据你的代码,这个函数会被触发

const editItem = (item: any) => {
    setFile(undefined);
    setComment(item.comment);
    setAmount(item.amount);
};

Now this function is updating the state and inside the input tags you are using the state as values, so this is the reason why you are getting the results you are getting现在这个函数正在更新状态并且在输入标签内你使用状态作为值,所以这就是你得到你得到的结果的原因

Now you can change your code to现在您可以将代码更改为

const editItem = (item) => {
     setItems(items.map(i => i.id === item.id ? {...i, comment, amount} : i);
    setComment("");
    setAmount("");
};

看起来您刚刚忘记向按钮添加 onclick 处理程序:

  <button formAction="submit" onClick={handleAddSubmit}>Submit</Button>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM