diff --git a/src/components/MyRootContext/index.js b/src/components/MyRootContext/index.js index 7699aef..8f99ad0 100644 --- a/src/components/MyRootContext/index.js +++ b/src/components/MyRootContext/index.js @@ -6,6 +6,14 @@ import dayjs from "dayjs"; export const MyRootContext = createContext(); export const UPDATE_SEARCH = "UPDATE_SEARCH"; // 定义初始状态和 reducer 函数 +function getInitialState(){ + let searchString = localStorage.getItem("huayu-todo-search"); + if (searchString) { + return JSON.parse(searchString); + } + return initialState; +} + const initialState = {"search":{ "pageSize": 20, "pageNumber": 1, @@ -75,6 +83,7 @@ const initialState = {"search":{ function reducer(state, action) { switch (action.type) { case UPDATE_SEARCH: + localStorage.setItem("huayu-todo-search", JSON.stringify({search:action.search})); return { ...state, search:action.search}; case 'decrement': return { count: state.count - 1 }; @@ -85,7 +94,7 @@ function reducer(state, action) { // 创建一个 Provider 组件 export function MyRootProvider({ children }) { - const [state, dispatch] = useReducer(reducer, initialState); + const [state, dispatch] = useReducer(reducer, getInitialState()); return ( {children}