我在reducer中有一个状态,它是按以下方式构建的:
state = {
companies: ['company1', 'company2', 'company3', 'company4']
}
这些公司是从API获取并添加到州。
我想从localStorage中删除仅在x小时前或之前添加到州的公司。我应该如何正确地完成这项任务?
我的localStorage.js:
export const loadState = () => {
try {
const serializedState = localStorage.getItem('state')
if (serializedState === null) {
return undefined;
}
return JSON.parse(serializedState)
} catch (err) {
return undefined
}
}
export const saveState = (state) => {
try {
const serializedState = JSON.stringify(state)
localStorage.setItem('state', serializedState)
} catch (err) {
// to define
}
}
保存状态发生在redcuer的CASE中,如下所示:
case FETCH_FROM_API:
return {
...state,
companies: [payload, ...state.companies]
};