【发布时间】:2021-09-06 23:44:55
【问题描述】:
我正在尝试使用 React Hooks 从我的状态中删除一个项目。我不知道我做错了什么。
我的数据如下所示,我正在尝试删除“items”数组中的单个项目。
const MYDATA = {
id: '0001',
title: 'A good title',
items: [
{
itemid: 0,
title: 'Cheddar',
variants: [
{ id: '062518', grams: 200, price: 3.00},
{ id: '071928', grams: 400, price: 5.50},
]
},
{
itemid: 1,
title: 'Edam',
variants: [
{ id: '183038', grams: 220, price: 2.50},
{ id: '194846', grams: 460, price: 4.99},
]
},
{
itemid: 2,
title: 'Red Leicester',
variants: [
{ id: '293834', grams: 420, price: 4.25},
{ id: '293837', grams: 660, price: 5.99},
]
}
]
}
当我单击“删除”按钮时,地图会中断,我认为这是因为我的状态格式不正确,但我不确定。
我面临的问题的一个例子可以在这里看到 - https://codesandbox.io/s/frosty-joliot-4wp1q?file=/src/App.js
任何帮助将不胜感激。
【问题讨论】:
-
请在问题描述中包含minimal reproducible example。代码沙箱很好,但不需要回答问题。
-
要从状态内的数组中删除项目,请参阅:How to delete an item from state array?
标签: javascript arrays reactjs react-hooks