From b8e16df389c42a99dffb7285f50a5689872e35ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E6=B3=A5?= <1656081615@qq.com> Date: Tue, 21 Mar 2023 18:58:46 +0800 Subject: [PATCH] Revert "fix(useStorageState): default value is not stored (#2064)" and "test(useStorageState): add test case (#2106)" (#2130) * Revert "fix(useStorageState): default value is not stored (#2064)" This reverts commit 204226285fcca0ba3cdd9da0a3c8f1a4853a0e28. * Revert "test(useStorageState): add test case (#2106)" This reverts commit 37749a3a9a6927162e4c68146c9e06a0dffb32f9. --- .../hooks/src/createUseStorageState/index.ts | 38 +++++++------------ .../__tests__/index.test.ts | 10 ----- 2 files changed, 14 insertions(+), 34 deletions(-) diff --git a/packages/hooks/src/createUseStorageState/index.ts b/packages/hooks/src/createUseStorageState/index.ts index 2438b092b2..83696798b8 100644 --- a/packages/hooks/src/createUseStorageState/index.ts +++ b/packages/hooks/src/createUseStorageState/index.ts @@ -42,22 +42,6 @@ export function createUseStorageState(getStorage: () => Storage | undefined) { return JSON.parse(value); }; - function getDefaultValue() { - return isFunction(options?.defaultValue) ? options?.defaultValue() : options?.defaultValue; - } - - function setStoredValue(value?: T) { - if (isUndef(value)) { - storage?.removeItem(key); - } else { - try { - storage?.setItem(key, serializer(value)); - } catch (e) { - console.error(e); - } - } - } - function getStoredValue() { try { const raw = storage?.getItem(key); @@ -67,12 +51,10 @@ export function createUseStorageState(getStorage: () => Storage | undefined) { } catch (e) { console.error(e); } - - const defaultValue = getDefaultValue(); - - setStoredValue(defaultValue); - - return defaultValue; + if (isFunction(options?.defaultValue)) { + return options?.defaultValue(); + } + return options?.defaultValue; } const [state, setState] = useState(() => getStoredValue()); @@ -83,9 +65,17 @@ export function createUseStorageState(getStorage: () => Storage | undefined) { const updateState = (value: T | IFuncUpdater) => { const currentState = isFunction(value) ? value(state) : value; - setState(currentState); - setStoredValue(currentState); + + if (isUndef(currentState)) { + storage?.removeItem(key); + } else { + try { + storage?.setItem(key, serializer(currentState)); + } catch (e) { + console.error(e); + } + } }; return [state, useMemoizedFn(updateState)] as const; diff --git a/packages/hooks/src/useLocalStorageState/__tests__/index.test.ts b/packages/hooks/src/useLocalStorageState/__tests__/index.test.ts index 94be1c564a..d7151a52ce 100644 --- a/packages/hooks/src/useLocalStorageState/__tests__/index.test.ts +++ b/packages/hooks/src/useLocalStorageState/__tests__/index.test.ts @@ -1,6 +1,5 @@ import { renderHook, act } from '@testing-library/react'; import useLocalStorageState from '../index'; -import 'jest-localstorage-mock'; describe('useLocalStorageState', () => { const setUp = (key: string, value: T) => @@ -107,13 +106,4 @@ describe('useLocalStorageState', () => { }); expect(hook.result.current.state).toBe('hello world, zhangsan'); }); - - it('should save the default value in localStorage', () => { - const LOCAL_STORAGE_KEY = 'test-default-value-key'; - const defaultValue = 'Hello'; - const hook = setUp(LOCAL_STORAGE_KEY, defaultValue); - expect(hook.result.current.state).toBe(defaultValue); - const localStorageValue = localStorage.getItem(LOCAL_STORAGE_KEY); - expect(localStorageValue).toBe(JSON.stringify(defaultValue)); - }); });