fix stickyState

This commit is contained in:
lingdocs 2021-09-18 19:23:41 -04:00
parent 0ccf744575
commit 26690a7ebc
2 changed files with 8 additions and 3 deletions

View File

@ -34,6 +34,7 @@
},
"scripts": {
"start": "react-scripts start",
"start-w-user": "REACT_APP_ENV=dev react-scripts start",
"build": "node scripts/get-words.js && react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",

View File

@ -7,9 +7,13 @@ export default function useStickyState<T>(defaultValue: T, key: string): {
const [value, setValue] = useState<T>(() => {
const stickyValue = window.localStorage.getItem(key);
return stickyValue !== null
? JSON.parse(stickyValue) as T
: defaultValue;
if (stickyValue === null) return defaultValue;
try {
return JSON.parse(stickyValue) as T;
} catch(e) {
console.error(e);
return defaultValue;
}
});
useEffect(() => {