From ba13554953c68843bfb8201815f8d9746ceeb2ea Mon Sep 17 00:00:00 2001 From: kpCoder Date: Thu, 3 Jul 2025 09:08:50 +0700 Subject: [PATCH] fix(useLocalStorage): fix setter function returns incorrect previous value --- src/useLocalStorage.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/useLocalStorage.ts b/src/useLocalStorage.ts index def6ad804a..e37ff8a24f 100644 --- a/src/useLocalStorage.ts +++ b/src/useLocalStorage.ts @@ -19,13 +19,14 @@ const useLocalStorage = ( if (!isBrowser) { return [initialValue as T, noop, noop]; } + if (!key) { throw new Error('useLocalStorage key may not be falsy'); } const deserializer = options ? options.raw - ? (value) => value + ? (value: string) => value : options.deserializer : JSON.parse; @@ -79,7 +80,8 @@ const useLocalStorage = ( // localStorage can throw. Also JSON.stringify can throw. } }, - [key, setState] + // eslint-disable-next-line react-hooks/exhaustive-deps + [key, setState, state] ); // eslint-disable-next-line react-hooks/rules-of-hooks