Skip to content

Commit

Permalink
Revert "fix(useStorageState): default value is not stored (#2064)" an…
Browse files Browse the repository at this point in the history
…d "test(useStorageState): add test case (#2106)" (#2130)

* Revert "fix(useStorageState): default value is not stored (#2064)"

This reverts commit 2042262.

* Revert "test(useStorageState): add test case (#2106)"

This reverts commit 37749a3.
  • Loading branch information
liuyib committed Mar 21, 2023
1 parent 3ff9bd3 commit b8e16df
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 34 deletions.
38 changes: 14 additions & 24 deletions packages/hooks/src/createUseStorageState/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<T>(() => getStoredValue());
Expand All @@ -83,9 +65,17 @@ export function createUseStorageState(getStorage: () => Storage | undefined) {

const updateState = (value: T | IFuncUpdater<T>) => {
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;
Expand Down
10 changes: 0 additions & 10 deletions packages/hooks/src/useLocalStorageState/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { renderHook, act } from '@testing-library/react';
import useLocalStorageState from '../index';
import 'jest-localstorage-mock';

describe('useLocalStorageState', () => {
const setUp = <T>(key: string, value: T) =>
Expand Down Expand Up @@ -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));
});
});

0 comments on commit b8e16df

Please sign in to comment.