undefined

bokuweb.me

ReduxでReducerでのstateの変更がviewに通知されないケースについてのメモ

知らなかったので完全にハマった。おぼえ書き。 以下のエントリに記載があった。

qiita.com

例えばこんなreducerの場合、viewが変更されない。

function someReducer(state, action) {
  switch (action.type) {
  case SOME_ACTION:
    state.someValue = action.someValue
    return state;
  default:
    return state;
  }
}

この例ではassignでstateを作りなおしている。

function someReducer(state, action) {
  switch (action.type) {
  case SOME_ACTION:
    return Object.assign({}, state, {
      someValue: action.someValue
    });
  default:
    return state;
  }
}

※15.11.3 17:11追記

なんかこれでいいんじゃないかって気がしてきてる。データが大きくなると新しいstateを用意してassignするのが面倒なケースもあるので、もらったstateに追加して複製してやる。

function someReducer(state, action) {
  switch (action.type) {
  case SOME_ACTION:
    state.someValue = action.someValue;
    return Object.assign({}, state);
  default:
    return state;
  }
}