您没有正确应用不可变更新模式,您正在改变指向当前状态对象的嵌套引用。您需要创建新的对象引用并浅拷贝所有正在更新的状态。
UPDATE_RESTRICTION: (curState, payload) => {
const updatedNodes = { ...curState.layout }
const accessProfile = BpUAE.accessProfileID;
payload.nodes.forEach((node, index) => {
if (typeof (updatedNodes[node].settings.bp_uae_restrictions) === 'undefined') {
updatedNodes[node] = {
...updatedNodes[node],
settings: {
...updatedNodes[node].settings,
bp_uae_restrictions: {},
},
};
}
if (typeof (updatedNodes[node].settings.bp_uae_restrictions[accessProfile]) === 'undefined') {
updatedNodes[node] = {
...updatedNodes[node],
settings: {
...updatedNodes[node].settings,
bp_uae_restrictions: {
...updatedNodes[node].settings.bp_uae_restrictions,
[accessProfile]: {},
},
},
};
}
// now all the new references have been created and previous
// state shallow copied, you can update the deeply nested
// `is_node_restricted` property.
updatedNodes[node].settings.bp_uae_restrictions[accessProfile].is_node_restricted = JSON.parse(payload.isRestricted);
});
return {
...curState,
layout: updatedNodes,
};
}
更新:添加了最后一个不可变模式
const updatedNodes = { ...curState.layout }
const accessProfile = BpUAE.accessProfileID;
payload.nodes.forEach((node, index) => {
if (typeof (updatedNodes[node].settings.bp_uae_restrictions) === 'undefined') {
updatedNodes[node] = {
...updatedNodes[node],
settings: {
...updatedNodes[node].settings,
bp_uae_restrictions: {},
},
};
}
if (typeof (updatedNodes[node].settings.bp_uae_restrictions[accessProfile]) === 'undefined') {
updatedNodes[node] = {
...updatedNodes[node],
settings: {
...updatedNodes[node].settings,
bp_uae_restrictions: {
...updatedNodes[node].settings.bp_uae_restrictions,
[accessProfile]: {},
},
},
};
}
// now all the new references have been created and previous
// state shallow copied, you can update the deeply nested
// `is_node_restricted` property.
updatedNodes[node] = {
...updatedNodes[node],
settings: {
...updatedNodes[node].settings,
bp_uae_restrictions: {
...updatedNodes[node].settings.bp_uae_restrictions,
[accessProfile]: {
...updatedNodes[node].settings.bp_uae_restrictions[accessProfile],
is_node_restricted: JSON.parse(payload.isRestricted)
},
},
},
};
});
return {
...curState,
layout: updatedNodes,
};
}```