React-redux component does not rerender on store state change

后端 未结 2 1186
轻奢々
轻奢々 2020-12-09 09:31

I\'m stating to learn react and redux today, yet I cannot figure out how to force component to rerender after state change.

Here is my code:

const s         


        
2条回答
  •  醉话见心
    2020-12-09 09:44

    I ended up here because I had written a bad reducer. I had:

    const reducer = (state=initialState, action) => {
      switch (action.type) {
        case 'SET_Q':
          return Object.assign(state, {                     // <- NB no {}!
            q: action.data,
          })
    
        default:
          return state;
      }
    }
    

    I needed:

    const reducer = (state=initialState, action) => {
      switch (action.type) {
        case 'SET_Q':
          return Object.assign({}, state, {                 // <- NB the {}!
            q: action.data,
          })
    
        default:
          return state;
      }
    }
    

提交回复
热议问题