为每个项目设置一个值,除了通过 Immutable JS 的项目

Set a value for every item except the item passed Immutable JS

我从 ImmutableJS 开始,我有以下 redux:

import { fromJS } from 'immutable';

import {
    SET_MODAL_VISIBLE
} from './constants';

const initialState = fromJS({
  loginModal: 'none',
  registerModal: 'none',
  passwordModal: 'none',
});

function modalReducer(state = initialState, action) {
  switch (action.type) {
    case SET_MODAL_VISIBLE:
      return state
        .set(action.modal, 'block');
    default:
      return state;
  }
}

export default modalReducer;

除了 "action.modal",我如何支持 'none' 的所有州项目?我知道我可以使用

return initialState
       .set(action.modal, 'block');

但是使用initialState对我来说不好,因为状态会比较多,我不想回到初始状态。

我假设 action.model 是一个字符串,因此您可以从您的状态中获取所有键并将它们设置为 "none" 除了 action.model 键使用 reduce:

[...state.keys()].reduce(
  (state,key)=>
    (key===action.model)
      ? state.set(key,"block")
      : state.set(key,"none"),
  state
);