Polymer / pwa-helpers

Small helper methods or mixins to help you build web apps.
BSD 3-Clause "New" or "Revised" License
439 stars 48 forks source link

connect-mixin _stateChanged method gives back a mutable state object #51

Closed araker closed 5 years ago

araker commented 5 years ago

Hello, I'm using the connect-mixin.js. I had a problem where updates in the redux store didn't persist to the underlying cache. It turns out that the stateChanged method gives back the redux state object directly (without doing a deep copy). So when the state is directly modified, the redux object is also modified, bypassing all reducers.

In my case this resulted that when the reducer was called, the values in the store were the same as in the action of the reducer. Redux-persist therefore didn't update the cache, because no changes were found.

I think it would be better if stateChanged gives back a deep copy (JSON.parse(JSON.stringify(data)). Since redux is a immutable data store, where only the reducers can alter the state, I don't expect that mutating a value from a observer would manipulate the state directly.

A simple example

_stateChanged(state)
{
  let myUser = state.user;

  //redux internal state object is also mutated by this 
  myUser.name = 'otherName'; 
}
araker commented 5 years ago

Closing this, because the scope of this issue is bigger than this library. Doing a (efficient) deep copy is still a hard problem in javascript.