我在redux中实现localStorage
export const loadState = () => {
try{
const serializedState = localStorage.getItem('state');
if(serializedState === null){
return undefined;
}
var result = JSON.parse(serializedState);
delete result['router'];
return result;
}
catch(err) {
return undefined;
}
};
export const saveState = (state) => {
try{
const serializedState = JSON.stringify(state);
localStorage.setItem('state', serializedState);
}
catch(err)
{
return undefined;
}
};
我正在设置初始状态并像这样更新状态
initialState = loadState();
const store = createStore(
makeRootReducer(),
initialState,
compose(
applyMiddleware(...middleware),
...enhancers
)
)
store.subscribe(() => {
saveState(store.getState());
});
刷新前的商店对象
并刷新后
我面临的问题是页面刷新状态不会保留所有键,而只保留当前路由中存在的键。我做错了什么(已编辑) 我有什么特别的渠道可以问这个问题