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