feat:查询放入localStorage

This commit is contained in:
1708-huayu 2025-01-26 16:23:19 +08:00
parent af93a92e93
commit d8aa521394
1 changed files with 10 additions and 1 deletions

View File

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