React-redux store updates but React does not

后端 未结 1 532
误落风尘
误落风尘 2020-12-09 01:44

Bear with me here as this question pertains to my first test app using either React, Redux or react-redux. Docs have gotten me far and I have a mock banking app that mostly

相关标签:
1条回答
  • 2020-12-09 02:03

    The problem is in this piece of code:

    // clone the state
    var newState = Object.assign({}, state);         
    
    // add the new transaction
    
    newState.accounts[accountIndex].transactions.unshift(action.payload.transaction);
    
    // update account balance
    newState.accounts[accountIndex].balance = action.payload.balance;
    

    Cloning the state object doesn't mean you can mutate the objects it is referring to. I suggest you to read more about immutability because this isn't how it works.

    This problem and solution to it are described in detail in Redux “Troubleshooting” docs so I suggest you to read them.

    https://redux.js.org/troubleshooting

    I also suggest you to take a look at Shopping Card example in Flux Comparison for Redux because it shows how to update nested objects without mutating them in a similar way to what you are asking.

    https://github.com/voronianski/flux-comparison/tree/master/redux

    0 讨论(0)
提交回复
热议问题