Skip to content

Commit

Permalink
feat(clayui.com): Add try catch around localStorage to make sure the …
Browse files Browse the repository at this point in the history
…site works when it's disabled
  • Loading branch information
kresimir-coko authored and bryceosterhaus committed Aug 12, 2020
1 parent eb723de commit b3f8558
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions clayui.com/src/components/Hooks/useStickyState.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,23 @@ const localStorage = {

function useStickyState(defaultValue, key) {
const [value, setValue] = React.useState(() => {
const stickyValue = localStorage.getItem(key);
let stickyValue;

try {
stickyValue = localStorage.getItem(key);
} catch (error) {
stickyValue = null;
}

return stickyValue !== null ? JSON.parse(stickyValue) : defaultValue;
});

React.useEffect(() => {
localStorage.setItem(key, JSON.stringify(value));
try {
localStorage.setItem(key, JSON.stringify(value));
} catch (error) {
return;
}
}, [key, value]);

return [value, setValue];
Expand Down

0 comments on commit b3f8558

Please sign in to comment.