LoginSignup
0

More than 5 years have passed since last update.

Reduxでdispatchしてもstoreが更新されない時はreducerを確認

Last updated at Posted at 2018-05-07

dispathcしているのにstoreが更新されない時

< もともと書いていたReducer>

export default function postListReducer(state = [], action) {
  switch (action.type) {
    case ActionType.FETCH_POST_LIST:
      return action.postList.reduce((accumulator, currentV) => {
        accumulator.push(currentV);
          return accumulator;
       }, state);
    default:
      return state;
  }
}

< 修正したReducer >

export default function postListReducer(state = [], action) {
  switch (action.type) {
    case ActionType.FETCH_POST_LIST:
      return [...state, ...action.postList];
    default:
      return state;
  }
}

元のコードだともともとのstateを変更してしまっています。
Reducerでは新規にオブジェクトや配列を返す必要があるので、正しく動作していませんでした。
(State自体は変更されるが、componentWillReceivePropsが動作しない。)

以上。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0