Reactjs 重置不更改状态形状的redux增强器
我创建了以下redux增强器,可以随时拍摄状态快照并回滚:Reactjs 重置不更改状态形状的redux增强器,reactjs,redux,react-redux,Reactjs,Redux,React Redux,我创建了以下redux增强器,可以随时拍摄状态快照并回滚: export const resetable = (reducer: Function) => { const initialState = { current: reducer(undefined, {}), previous: undefined }; return (state = initialState, action: Action) => { switch (action.type) {
export const resetable = (reducer: Function) => {
const initialState = { current: reducer(undefined, {}), previous: undefined };
return (state = initialState, action: Action) => {
switch (action.type) {
case ActionTypes.SaveSnapShot:
return { ...state, previous: state.current };
case ActionTypes.Rollback:
return { ...state, current: state.previous, previous: undefined };
case ActionTypes.Commit:
return { ...state, previous: undefined };
default:
return {
...state,
current: reducer(state.current, action)
};
}
};
};
其用法如下:
export default function createReducer() {
return combineReducers({
key: resetable(reducer),
});
}
{
contact: {
firstName: 'Bob',
lastName: 'Conman'
}
}
问题是,它会改变传入减速器状态的形状
例如,如果我有这样的状态:
export default function createReducer() {
return combineReducers({
key: resetable(reducer),
});
}
{
contact: {
firstName: 'Bob',
lastName: 'Conman'
}
}
然后它将当前
和上一个
键添加到状态
因此,国家成为:
{
contact: {
current: {
firstName: 'Bob',
lastName: 'Conman'
},
previous: undefined
}
}
在不改变状态形状和添加新密钥的情况下,是否仍可以提供类似的功能