简体   繁体   English

State 更新 Redux 值时检测到突变

[英]State Mutation detected when updating Redux Values

So I'm fairly new to React-Redux and I'm facing this problem where if i update this array with a new object in redux, i'm getting the following error所以我对 React-Redux 还很陌生,我遇到了这个问题,如果我在 redux 中用新的 object 更新这个数组,我会收到以下错误

Uncaught Invariant Violation: A state mutation was detected between dispatches, in the path `settingsObject.arrayForSettings.0.updatedObject`. This may cause incorrect behavior.

The Problem only arises when i update the state in redux with new values, On previous values there is no error.仅当我使用新值更新 redux 中的 state 时才会出现问题,在以前的值上没有错误。 The piece of code that is giving me the error is as follows.给我错误的代码如下。

let tempArrayForSettings = [...props.arrayForSettings];
tempArrayForSettings.forEach((element:any) => {
   if(element.settingsType === "DesiredSettingType")
   {
      //modify elements of a JSON object and add it to the element
      element.updatedObject = JSONUpdatedObject;
   }
   //call the action method to update the redux state
   updateAction(tempArrayForSettings);
});

The error is pointing me to the following link: Redux Error错误将我指向以下链接: Redux 错误

I know I'm not updating the object I'm getting from props instead I'm making a copy using the spread operator so I really don't know why the error is coming whenever I'm firing the updateAction function.我知道我没有更新 object 我是从道具中获取的,而是使用扩展运算符制作副本,所以我真的不知道为什么每当我触发 updateAction function 时都会出现错误。

Well, your line element.updatedObject = JSONUpdatedObject;好吧,你的 line element.updatedObject = JSONUpdatedObject; is modifying the object in the Redux store .正在修改Redux 商店中的 object element is a direct reference to your store object. element是对您的商店 object 的直接引用。 You would need to do an immutable copy here - your spread above only does a shallow copy, but the items here are still the same.你需要在这里做一个不可变的副本 - 你上面的传播只做一个浅拷贝,但这里的项目仍然是相同的。

Generally, you should do logic like this not in your component, but within your Reducer.一般来说,你不应该在你的组件中,而是在你的 Reducer 中做这样的逻辑。 (see the Style Guide on this topic ) That also gives you the benefit that you don't need to care about immutability, as within createSlice reducers you can simply modify data. (请参阅有关此主题的样式指南)这还为您提供了无需关心不变性的好处,因为在createSlice reducers 中您可以简单地修改数据。

You're updating the object inside the array, so the spread operator that you've created above only clones the array itself, not the objects inside the array.您正在更新数组内的 object,因此您在上面创建的扩展运算符仅克隆数组本身,而不是数组内的对象。 In Javascript, objects are passed by reference ( read more here )在 Javascript 中,对象通过引用传递( 在此处阅读更多内容

To fix this warning, you'd need to copy the object itself, and to do that, you'd need to find the specific object in the array that you'd like to change.要修复此警告,您需要复制 object 本身,为此,您需要在数组中找到要更改的特定 object。

Try this:尝试这个:

const { arrayForSettings } = props;
const modifiedSettings = arrayForSettings.map((element:any) => {
   if(element.settingsType === "DesiredSettingType")
   {
      //modify elements of a JSON object and add it to the element
      return {
        ...element,
        updatedObject: JSONUpdatedObject,
      }

   }
   return element;
}

   updateAction(modifiedSettings);
});

Also, it's recommended that this logic lives on the reducer side, not in your component.此外,建议此逻辑存在于 reducer 端,而不是您的组件中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM