From b3f8558f7b2831bb92ffa73f1a347cfba8a453f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kre=C5=A1imir=20=C4=8Coko?= Date: Tue, 11 Aug 2020 15:07:24 +0200 Subject: [PATCH] feat(clayui.com): Add try catch around localStorage to make sure the site works when it's disabled --- clayui.com/src/components/Hooks/useStickyState.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/clayui.com/src/components/Hooks/useStickyState.js b/clayui.com/src/components/Hooks/useStickyState.js index 90c6f0c227..19889d88e1 100644 --- a/clayui.com/src/components/Hooks/useStickyState.js +++ b/clayui.com/src/components/Hooks/useStickyState.js @@ -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];