fix stickyState
This commit is contained in:
parent
0ccf744575
commit
26690a7ebc
|
@ -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",
|
||||
|
|
|
@ -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(() => {
|
||||
|
|
Loading…
Reference in New Issue