From 842244e289265a31705a6f5d54c1e5e236a2d897 Mon Sep 17 00:00:00 2001 From: cha0s Date: Sun, 20 Mar 2022 03:14:56 -0500 Subject: [PATCH] fix: SF squad never knows best --- packages/core/src/hooks/use-local-storage.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/core/src/hooks/use-local-storage.js b/packages/core/src/hooks/use-local-storage.js index 283e49d..e143c24 100644 --- a/packages/core/src/hooks/use-local-storage.js +++ b/packages/core/src/hooks/use-local-storage.js @@ -3,13 +3,15 @@ import { useState, } from '@flecks/react'; +const useBrowserLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : () => {}; + const useLocalStorage = (key, defaultValue) => { const [value, setValue] = useState(defaultValue); - useLayoutEffect(() => { + useBrowserLayoutEffect(() => { setValue(JSON.parse(window.localStorage.getItem(key) || JSON.stringify(defaultValue))); // eslint-disable-next-line react-hooks/exhaustive-deps }, [key]); - useLayoutEffect(() => { + useBrowserLayoutEffect(() => { window.localStorage.setItem(key, JSON.stringify(value)); }, [key, value]); return [value, setValue];