如何为反应中的嵌套对象数组设置状态?
How to setState for nested array of object in react?
这是对象,假设我要在列表中添加一些内容:[],我该怎么做?我知道我们可以使用 prevList 回调来做到这一点,但我对通过它进行映射感到困惑。
const [boardlist, setBoardlist] = useState([
{
id: 1,
boardName: "home work",
data: [
{
id: 1,
header: "Stuff to do",
list: [
{
id: 1,
taskTitle: "working from home",
},
],
},
{
id: 2,
header: "In Progress",
list: [],
},
{
id: 3,
header: "Done",
list: [],
},
],
},
]);
如何复制状态并使用方括号和点表示法推送到您需要更改的元素?
const boardlistCopy = JSON.parse(JSON.stringify(boardlist));
boardlistCopy[0].data[1].list.push({id: 2, task: "studying React"});
setBoardlist(boardlistCopy);
这是对象,假设我要在列表中添加一些内容:[],我该怎么做?我知道我们可以使用 prevList 回调来做到这一点,但我对通过它进行映射感到困惑。
const [boardlist, setBoardlist] = useState([
{
id: 1,
boardName: "home work",
data: [
{
id: 1,
header: "Stuff to do",
list: [
{
id: 1,
taskTitle: "working from home",
},
],
},
{
id: 2,
header: "In Progress",
list: [],
},
{
id: 3,
header: "Done",
list: [],
},
],
},
]);
如何复制状态并使用方括号和点表示法推送到您需要更改的元素?
const boardlistCopy = JSON.parse(JSON.stringify(boardlist));
boardlistCopy[0].data[1].list.push({id: 2, task: "studying React"});
setBoardlist(boardlistCopy);