From d8aa5213942d04c2d5b4734d677ad5e7ac7e1d2e Mon Sep 17 00:00:00 2001 From: 1708-huayu <57060237+1708-huayu@users.noreply.github.com> Date: Sun, 26 Jan 2025 16:23:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=9F=A5=E8=AF=A2=E6=94=BE=E5=85=A5localS?= =?UTF-8?q?torage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/MyRootContext/index.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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}