React-redux: reducer w/ array of items doesn't re-render when item in array is updated.

Created on 23 Sep 2016  路  3Comments  路  Source: reduxjs/react-redux

posted a question on Stack Overflow, but I think it _may_ be a bug in react-redux (or maybe I'm just a dum dum) :)

http://stackoverflow.com/questions/39667805/react-redux-update-item-in-array-doesnt-re-render

Most helpful comment

You're directly mutating the state by calling push on the array instance. Since the array reference is the same before and after, connect assumes nothing has changed and doesn't re-render. Please see the Redux FAQ on the topic: http://redux.js.org/docs/FAQ.html#react-not-rerendering

All 3 comments

You're directly mutating the state by calling push on the array instance. Since the array reference is the same before and after, connect assumes nothing has changed and doesn't re-render. Please see the Redux FAQ on the topic: http://redux.js.org/docs/FAQ.html#react-not-rerendering

Thanks!

keep in mind that having a list within an object it's not enough to use Object.assign an copy the object, also the list needs to be copied separately e.g. using slice()

e.g.

 const initialState = {
     customers: [],
     date: null,
     states: ['state1', 'state2']
 };

//.... and in reducer:

 case SET_STATES:
            return {
                ...state,
                states: action.states.slice()
            };
Was this page helpful?
0 / 5 - 0 ratings