Skip to content

Commit 289989f

Browse files
authored
chore: performance optimization (#2913)
* chore: performance optimization * chore: performance optimization * fix: update * fix: update
1 parent b6d5380 commit 289989f

File tree

1 file changed

+12
-9
lines changed
  • packages/hooks/src/createUseStorageState

1 file changed

+12
-9
lines changed

packages/hooks/src/createUseStorageState/index.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export interface Options<T> {
1616
onError?: (error: unknown) => void;
1717
}
1818

19-
export function createUseStorageState(getStorage: () => Storage | undefined) {
20-
function useStorageState<T>(key: string, options: Options<T> = {}) {
19+
export const createUseStorageState = (getStorage: () => Storage | undefined) => {
20+
const useStorageState = <T>(key: string, options: Options<T> = {}) => {
2121
let storage: Storage | undefined;
2222

2323
const { listenStorageChange = false } = options;
@@ -35,7 +35,7 @@ export function createUseStorageState(getStorage: () => Storage | undefined) {
3535
onError(err);
3636
}
3737

38-
function getStoredValue() {
38+
const getStoredValue = () => {
3939
try {
4040
const raw = storage?.getItem(key);
4141
if (raw) {
@@ -48,7 +48,7 @@ export function createUseStorageState(getStorage: () => Storage | undefined) {
4848
return options.defaultValue();
4949
}
5050
return options.defaultValue;
51-
}
51+
};
5252

5353
const [state, setState] = useState<T>(getStoredValue);
5454

@@ -57,6 +57,9 @@ export function createUseStorageState(getStorage: () => Storage | undefined) {
5757

5858
useUpdateEffect(() => {
5959
const nextState = getStoredValue();
60+
if (Object.is(nextState, stateRef.current)) {
61+
return; // 新旧状态相同,不更新 state,避免 setState 带来不必要的 re-render
62+
}
6063
stateRef.current = nextState;
6164
setState(nextState);
6265
}, [key]);
@@ -66,7 +69,7 @@ export function createUseStorageState(getStorage: () => Storage | undefined) {
6669
const currentState = isFunction(value) ? value(previousState) : value;
6770

6871
if (Object.is(currentState, previousState)) {
69-
return;
72+
return; // 新旧状态相同,不更新 state,避免 setState 带来不必要的 re-render
7073
}
7174

7275
if (!listenStorageChange) {
@@ -112,7 +115,7 @@ export function createUseStorageState(getStorage: () => Storage | undefined) {
112115
const nextState = getStoredValue();
113116

114117
if (Object.is(nextState, stateRef.current)) {
115-
return;
118+
return; // 新旧状态相同,不更新 state,避免 setState 带来不必要的 re-render
116119
}
117120

118121
stateRef.current = nextState;
@@ -133,8 +136,8 @@ export function createUseStorageState(getStorage: () => Storage | undefined) {
133136
enable: listenStorageChange,
134137
});
135138

136-
return [state, useMemoizedFn(updateState) as (value: SetState<T>) => void] as const;
137-
}
139+
return [state, useMemoizedFn(updateState)] as const;
140+
};
138141

139142
return useStorageState;
140-
}
143+
};

0 commit comments

Comments
 (0)