fix useUser
This commit is contained in:
parent
68cf6f3f87
commit
0ccf744575
|
@ -1,6 +0,0 @@
|
|||
import React from "react";
|
||||
import { AT } from "@lingdocs/lingdocs-main";
|
||||
|
||||
const UserContext = React.createContext<undefined | AT.LingdocsUser>(undefined);
|
||||
|
||||
export default UserContext;
|
|
@ -1,17 +1,20 @@
|
|||
import { useEffect, useState } from "react";
|
||||
|
||||
export default function useStickyState<T>(defaultValue: T, key: string): [T, React.Dispatch<React.SetStateAction<T>>] {
|
||||
export default function useStickyState<T>(defaultValue: T, key: string): {
|
||||
value: T,
|
||||
setValue: React.Dispatch<React.SetStateAction<T>>,
|
||||
} {
|
||||
|
||||
const [value, setValue] = useState<T>(() => {
|
||||
const stickyValue = window.localStorage.getItem(key);
|
||||
return stickyValue !== null
|
||||
? JSON.parse(stickyValue) as T
|
||||
: defaultValue;
|
||||
});
|
||||
const [value, setValue] = useState<T>(() => {
|
||||
const stickyValue = window.localStorage.getItem(key);
|
||||
return stickyValue !== null
|
||||
? JSON.parse(stickyValue) as T
|
||||
: defaultValue;
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
window.localStorage.setItem(key, JSON.stringify(value));
|
||||
}, [key, value]);
|
||||
useEffect(() => {
|
||||
window.localStorage.setItem(key, JSON.stringify(value));
|
||||
}, [key, value]);
|
||||
|
||||
return [value, setValue];
|
||||
return {value, setValue};
|
||||
}
|
|
@ -14,7 +14,10 @@ const UserContext = createContext<
|
|||
|
||||
// TODO: persisting user in local state
|
||||
function UserProvider({ children }: any) {
|
||||
const [value, setValue] = useStickyState<AT.LingdocsUser | undefined>(undefined, "saved-user");
|
||||
const {value, setValue} = useStickyState<AT.LingdocsUser | undefined>(
|
||||
undefined,
|
||||
"saved-user",
|
||||
);
|
||||
|
||||
function pullUser() {
|
||||
getUser().then((user) => {
|
||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue