Skip to content

Feature: resetState method #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/usePersistStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ const usePersistStorage = <Value>(
migrate = defaultOptions.migrate,
sensitive = defaultOptions.sensitive
}: UsePersistStorageOptions<Value> = defaultOptions
): [Value, AsyncSetState<Value>, boolean] => {
): [
Value,
AsyncSetState<Value>,
boolean,
() => Promise<void>
] => {
const currentVersion = useRef<number>(version || 0);
const [state, setState] = useState<Value>(initialValue);
const [restored, setRestored] = useState<boolean>(false);
Expand Down Expand Up @@ -148,7 +153,15 @@ const usePersistStorage = <Value>(
}
};

return [state, asyncSetState, restored];
const resetState = async () => {
const newValue: Value =
initialValue instanceof Function
? initialValue()
: initialValue;
await asyncSetState(newValue);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better if we can wrap useCallback, and reduce the dependency.
I will suggest you re-write to like below...

const resetState = useCallback(async () => {
    const newValue: Value =
      initialValue instanceof Function
        ? initialValue()
        : initialValue;
  
    setState(newValue);
    if (persist) {
      await setValueToStorage(newValue);
    }
  }, [initialValue, persist, setValueToStorage]);

}

return [state, asyncSetState, restored, resetState];
};

export default usePersistStorage;
61 changes: 60 additions & 1 deletion tests/usePersistStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ test("init, setState, version = 10", async () => {
});
expect(result.current[0]).toBe("async change callback");
expect(await store.getItem(KEY)).toBe(toStorageValue("async change callback", 10));

});

test("restore state", async () => {
Expand Down Expand Up @@ -78,3 +77,63 @@ test("no persist state", async () => {
expect(result.current[0]).toBe("change");
expect(await store.getItem(KEY)).toBeNull();
});

test("init, set state, reset state (when initialValue is a Value)", async () => {
const initialValue = "my initial value"
const { result } = renderHook(() =>
usePersistStorage(KEY, initialValue, { version: 10 })
);

expect(result.current[0]).toBe(initialValue);
expect(typeof result.current[1]).toBe("function");
expect(result.current[2]).toBe(false);

await sleep(100); // wait mount && init asyncStorage;
expect(await store.getItem(KEY)).toBe(toStorageValue(initialValue, 10));
expect(result.current[2]).toBe(true);

act(async () => {
await result.current[1]("change");
});
expect(result.current[0]).toBe("change");

await sleep(100); // wait update asyncStorage;
expect(await store.getItem(KEY)).toBe(toStorageValue("change", 10));

act(async () => { // reset state
await result.current[3]();
});
expect(result.current[0]).toBe(initialValue);
await sleep(100); // wait update asyncStorage;
expect(await store.getItem(KEY)).toBe(toStorageValue(initialValue, 10));
});

test("init, set state, reset state (when initialValue is a function)", async () => {
const initialValue = () => "my initial value"
const { result } = renderHook(() =>
usePersistStorage(KEY, initialValue, { version: 10 })
);

expect(result.current[0]).toBe(initialValue());
expect(typeof result.current[1]).toBe("function");
expect(result.current[2]).toBe(false);

await sleep(100); // wait mount && init asyncStorage;
expect(await store.getItem(KEY)).toBe(toStorageValue(initialValue(), 10));
expect(result.current[2]).toBe(true);

act(async () => {
await result.current[1]("change");
});
expect(result.current[0]).toBe("change");

await sleep(100); // wait update asyncStorage;
expect(await store.getItem(KEY)).toBe(toStorageValue("change", 10));

act(async () => { // reset state
await result.current[3]();
});
expect(result.current[0]).toBe(initialValue());
await sleep(100); // wait update asyncStorage;
expect(await store.getItem(KEY)).toBe(toStorageValue(initialValue(), 10));
});