Skip to content

Commit

Permalink
fix(createUseStorageState): invoke setState twice (alibaba#2532)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyib authored Apr 25, 2024
1 parent 3ba7cb6 commit 4841696
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
6 changes: 5 additions & 1 deletion packages/hooks/src/createUseStorageState/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ export function createUseStorageState(getStorage: () => Storage | undefined) {

const updateState = (value?: SetState<T>) => {
const currentState = isFunction(value) ? value(state) : value;
setState(currentState);

if (!listenStorageChange) {
setState(currentState);
}

try {
let newValue: string | null;
Expand Down Expand Up @@ -126,5 +129,6 @@ export function createUseStorageState(getStorage: () => Storage | undefined) {

return [state, useMemoizedFn(updateState)] as const;
}

return useStorageState;
}
7 changes: 2 additions & 5 deletions packages/hooks/src/useLocalStorageState/demo/demo4.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,12 @@ function Counter() {
listenStorageChange: true,
});

const add = () => setCount(count! + 1);
const clear = () => setCount();

return (
<div style={{ marginBottom: '8px' }}>
<button style={{ marginRight: '8px' }} onClick={add}>
<button style={{ marginRight: '8px' }} onClick={() => setCount(count! + 1)}>
count: {count}
</button>
<button onClick={clear}>Clear</button>
<button onClick={() => setCount()}>Clear</button>
</div>
);
}

0 comments on commit 4841696

Please sign in to comment.