我从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”之外,我怎样才能将所有状态的项目都替换为“无”?我知道我可以使用
return initialState
.set(action.modal, 'block');
但是使用initialState对我不好,因为状态将是更多我不想回到初始状态的项目。
我假设action.model是一个字符串,所以你可以从你的状态获取所有键并将它们设置为“none”,除了使用reduce的action.model键:
[...state.keys()].reduce(
(state,key)=>
(key===action.model)
? state.set(key,"block")
: state.set(key,"none"),
state
);