feat:查询放入localStorage
This commit is contained in:
parent
af93a92e93
commit
d8aa521394
|
@ -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 (
|
||||
<MyRootContext.Provider value={{ state, dispatch }}>
|
||||
{children}
|
||||
|
|
Loading…
Reference in New Issue