【发布时间】:2021-01-17 22:15:48
【问题描述】:
我目前正在尝试更新基于 React 类的组件中的状态对象。
问题是我的状态是一个二维数组,看起来像这样:
this.state = {
items: [
{
title: 'first depth',
items: [
{
title: 'second depth',
},
],
},
],
};
为了更新我的状态对象中数组的第一个深度,我使用以下代码:
this.setState(({items}) => ({
items: [
...items.slice(0, index),
{
...items[index],
title: 'new Title',
},
...items.slice(index + 1),
],
}));
但我无法更新状态内数组的第二个深度。
有人知道如何更新第二个深度吗?
【问题讨论】:
-
您好,您需要一次更新所有元素吗?如果没有,您可以只获取第二个深层项目,更新其内容,然后更新主数组
标签: javascript reactjs