update: Set zustand storage.

mack-mac
mackt 8 months ago
parent 6d7172515e
commit a3def0adc4

@ -1,6 +1,7 @@
import { produce } from 'immer'; import { produce } from 'immer';
import { setCookie } from 'nookies'; import { setCookie } from 'nookies';
import { create } from 'zustand'; import { create } from 'zustand';
import { devtools, persist } from 'zustand/middleware';
import { apiGetMyUserInfo, apiUserLogin } from '@/services'; import { apiGetMyUserInfo, apiUserLogin } from '@/services';
@ -19,8 +20,10 @@ interface UserState {
getUserInfo: () => Promise<boolean>; getUserInfo: () => Promise<boolean>;
} }
// 创建状态存储 const useUserStore = create<UserState>()(
const useUserStore = create<UserState>((set) => { devtools(
persist(
(set) => {
const getUserInfo = async () => { const getUserInfo = async () => {
const result = await apiGetMyUserInfo(); const result = await apiGetMyUserInfo();
if (result.code === 200) await set(produce((state) => void (state.userInfo = result.data))); if (result.code === 200) await set(produce((state) => void (state.userInfo = result.data)));
@ -67,6 +70,13 @@ const useUserStore = create<UserState>((set) => {
return await getUserInfo(); return await getUserInfo();
}, },
}; };
}); },
{
name: 'user',
getStorage: () => localStorage,
},
),
),
);
export default useUserStore; export default useUserStore;

Loading…
Cancel
Save