我想保存状态中的更改,即使页面被重新加载。我已经在console.log中检查了我的oldState常量,它可以工作。但是我的用户界面不显示本地存储中的状态。我该如何在preloadedState中使用我的oldState常量?
const saveState = state => {
try {
const serialisedState = JSON.stringify(state)
window.localStorage.setItem('appState', serialisedState)
} catch (err) {
// error message
}
}
const loadState = () => {
try {
const serialisedState = window.localStorage.getItem('appState')
console.log(serialisedState)
if (!serialisedState) return undefined
} catch (err) {
// error message
return undefined
}
}
const oldState = loadState()
export const store = configureStore({
reducer: {
products,
filters,
product,
cart,
},
preloadedState: oldState,
})
store.subscribe(() => {
saveState(store.getState())
})
1条答案
按热度按时间laik7k3q1#
在您的
loadState
这一行之后您应该返回值
更新的
loadState
:希望能有所帮助。