使用redux中的动态键更新不可变状态

时间:2016-11-29 22:03:35

标签: redux immutable.js

我有这个initialState

const initialState = Immutable.fromJS({
  list: [],
  entities: {},
  something: {
    otherOne: false,
    otherTwo: false,
  },
});

我想用这样的函数更新对象something

[UPDATE_SOMETHING]: (state, { key }) => { // <== key is the name of the nested props
    return state.merge({
      filterBy: {[key]: !state.get('key')},
    });
  ),

但它不起作用......我如何动态更新状态?

1 个答案:

答案 0 :(得分:2)

[UPDATE_SOMETHING]: (state, {key}) => {
  const something = state.get('something').withMutations(s => {
    s.set(key, !s.get(key));
  });
  return state.set('something', something);
),

这样的事情会有所帮助吗?