mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-24 20:36:40 +07:00
fixed auth hydration
This commit is contained in:
parent
d9feff085a
commit
e90f8b6888
3 changed files with 14 additions and 3 deletions
|
@ -1,7 +1,7 @@
|
|||
import { IUser } from '~/types/auth';
|
||||
import { EMPTY_USER } from '~/constants/auth';
|
||||
import { makeAutoObservable } from 'mobx';
|
||||
import { makePersistable } from 'mobx-persist-store';
|
||||
import { makePersistable, isHydrated } from 'mobx-persist-store';
|
||||
|
||||
export class AuthStore {
|
||||
token: string = '';
|
||||
|
@ -18,6 +18,10 @@ export class AuthStore {
|
|||
});
|
||||
}
|
||||
|
||||
get isHydrated() {
|
||||
return isHydrated(this);
|
||||
}
|
||||
|
||||
get isUser() {
|
||||
return !!this.token;
|
||||
}
|
||||
|
|
|
@ -13,6 +13,10 @@ export class Store {
|
|||
constructor() {
|
||||
makeAutoObservable(this);
|
||||
}
|
||||
|
||||
get isHydrated() {
|
||||
return this.auth.isHydrated;
|
||||
}
|
||||
}
|
||||
|
||||
const defaultStore = new Store();
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
import React, { createContext, FC, useContext } from 'react';
|
||||
import { Store } from '~/store';
|
||||
import { observer } from 'mobx-react-lite';
|
||||
|
||||
export const StoreContext = createContext<Store>(new Store());
|
||||
|
||||
export const StoreContextProvider: FC<{ store: Store }> = ({ children, store }) => {
|
||||
export const StoreContextProvider: FC<{ store: Store }> = observer(({ children, store }) => {
|
||||
if (!store.isHydrated) return null;
|
||||
|
||||
return <StoreContext.Provider value={store}>{children}</StoreContext.Provider>;
|
||||
};
|
||||
});
|
||||
|
||||
export const useStore = () => useContext(StoreContext);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue