fix stickyState
This commit is contained in:
parent
0ccf744575
commit
26690a7ebc
|
@ -34,6 +34,7 @@
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "react-scripts start",
|
"start": "react-scripts start",
|
||||||
|
"start-w-user": "REACT_APP_ENV=dev react-scripts start",
|
||||||
"build": "node scripts/get-words.js && react-scripts build",
|
"build": "node scripts/get-words.js && react-scripts build",
|
||||||
"test": "react-scripts test",
|
"test": "react-scripts test",
|
||||||
"eject": "react-scripts eject",
|
"eject": "react-scripts eject",
|
||||||
|
|
|
@ -7,9 +7,13 @@ export default function useStickyState<T>(defaultValue: T, key: string): {
|
||||||
|
|
||||||
const [value, setValue] = useState<T>(() => {
|
const [value, setValue] = useState<T>(() => {
|
||||||
const stickyValue = window.localStorage.getItem(key);
|
const stickyValue = window.localStorage.getItem(key);
|
||||||
return stickyValue !== null
|
if (stickyValue === null) return defaultValue;
|
||||||
? JSON.parse(stickyValue) as T
|
try {
|
||||||
: defaultValue;
|
return JSON.parse(stickyValue) as T;
|
||||||
|
} catch(e) {
|
||||||
|
console.error(e);
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
Loading…
Reference in New Issue