1 回答

TA贡献1744条经验 获得超4个赞
尝试在这两个更新您的数组声明addNewModuleInLayout,并removeModule以
const newLayout = { ...this.state.layouts }; // from let newLayout = this.state.layouts
const newModule = [...this.state.modules]; // from let newModule = this.state.modules
在 cDM 中,您不会在状态上创建模块/布局数组的“新”副本,而是直接引用数组。当你更新数组时,你实际上改变了你的状态,所以当你prevState与this.state字符串化版本进行差异时是相等的。
这不会深入复制您的布局对象,因此在推送到布局数组时您会遇到同样的问题。您可以通过在更新布局而不是推送时创建新数组来解决此问题
newLayout[key] = [
...newLayout[key],
{
i: moduleData.type,
x: (newLayout[key].length * 1) % 3,
y: Infinity,
w: 1,
h: 5
}
]; // from newLayout[key].push({DATA}); in addNewModuleInLayout
因为你用filter在removeModule你那里应该很好
添加回答
举报