简体   繁体   English

在具有对象数组的reducer中更新redux状态

[英]Update the redux state in the reducer having array of objects

I am new to the react-redux. 我是React Redux的新手。

I do have an object which is like, 我确实有一个像

 const initialState = {
        Low: [
            {
                id: 0,
                technologyId: 0,
                technology: '',
                level: 'EASY'
            }
        ],
        Medium: [
            {
                id: 0,
                technologyId: 0,
                technology: '',
                level: 'MEDIUM'
            }
        ],
        High: [
            {
                id: 0,
                technologyId: 0,
                technology: '',
                level: 'TOUGH'
            }
        ]
    }

Now, 

    export default function QuizData(state = initialState, action) {
        switch (action.type) {
            case QUIZ_DATA:
                return {
                    ...state,
                    Low: action.data,
                    error: false,
                } 
            case RESET_SETUP_QUIZ: {
            console.log("intial state is ",  ...state);
            return {
                ...state
            }

Now, here what happens is after some manipulations, this objects gets changes with every key is having some values. 现在,这里发生的是经过一些操作后,此对象随每个键具有一些值而发生变化。 like, 喜欢,

{
        Low: [
            {
                id: 0,
                technologyId: 11,
                technology: 'xsxs',
                level: 'EASY'
            }
        ],
        Medium: [
            {
                id: 0,
                technologyId: 22,
                technology: 'swwsw',
                level: 'MEDIUM'
            }
        ],
        High: [
            {
                id: 0,
                technologyId: 110,
                technology: 'xsxsx',
                level: 'TOUGH'
            }
        ]
    }

So, This gets changed.Now, what I want to do is that , 所以,这变了。现在,我想做的是,

When user clicks a button that time I want to change this to the initial state. 当用户单击按钮时,我想将其更改为初始状态。

So that it will not have any values as it should be same as by default. 这样就不会有任何值,因为它应该与默认值相同。

SO, what I tried it 所以,我尝试了什么

return {
   initalState
}

But here, initial state is also having the same values. 但是在这里,初始状态也具有相同的值。

So, I am not getting a way to make it to the initial level. 因此,我没有办法使它达到初始水平。

Can one help me with this ? 有人可以帮我吗?

Because you use the original state object and return a modified version of it (ie you do affect your initialState). 因为您使用原始状态对象并返回它的修改版本(即,您确实会影响您的initialState)。

You must create a copy of the state in your reducer, for example 您必须在化简器中创建状态的副本,例如

case QUIZ_DATA:
  return Object.assign(
    {},
    state,
    {
      Low: action.data,
      error: false
    }
  )
case RESET_SETUP_QUIZ: {
  return Object.assign(
    {},
    state
  )
}

You have dedicated librairies like immutablejs to handle this ( https://redux.js.org/recipes/usingimmutablejs ) 您可以使用诸如immutablejs之类的专用库来处理此问题( https://redux.js.org/recipes/usingimmutablejs

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

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