Skip to content

Commit 9a358e2

Browse files
authored
fix(core): apply named function pattern to useEffect calls (#347)
1 parent 35d13f8 commit 9a358e2

6 files changed

Lines changed: 47 additions & 27 deletions

File tree

.changeset/pretty-candles-wink.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-simplikit': patch
3+
---
4+
5+
use named functions in useEffect callbacks for better stack traces

packages/core/src/hooks/useDebounce/useDebounce.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,14 @@ export function useDebounce<F extends (...args: any[]) => unknown>(
7373
return debounce<F>(preservedCallback, wait, { edges });
7474
}, [preservedCallback, wait, edges]);
7575

76-
useEffect(() => {
77-
return () => {
78-
debounced.cancel();
79-
};
80-
}, [debounced]);
76+
useEffect(
77+
function cancelDebouncedOnUnmount() {
78+
return () => {
79+
debounced.cancel();
80+
};
81+
},
82+
[debounced]
83+
);
8184

8285
return debounced;
8386
}

packages/core/src/hooks/useDebouncedCallback/useDebouncedCallback.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function useDebouncedCallback({
4242
const handleChange = usePreservedCallback(onChange);
4343
const ref = useRef({ value: false, clearPreviousDebounce: () => {} });
4444

45-
useEffect(() => {
45+
useEffect(function clearDebouncedOnUnmount() {
4646
const current = ref.current;
4747
return () => {
4848
current.clearPreviousDebounce();

packages/core/src/hooks/useInterval/useInterval.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,24 @@ export function useInterval(callback: () => void, options: IntervalOptions) {
4646

4747
const preservedCallback = usePreservedCallback(callback);
4848

49-
useEffect(() => {
50-
if (immediate === true && enabled) {
51-
preservedCallback();
52-
}
53-
}, [immediate, preservedCallback, enabled]);
49+
useEffect(
50+
function runImmediateCallback() {
51+
if (immediate === true && enabled) {
52+
preservedCallback();
53+
}
54+
},
55+
[immediate, preservedCallback, enabled]
56+
);
5457

55-
useEffect(() => {
56-
if (!enabled) {
57-
return;
58-
}
58+
useEffect(
59+
function startInterval() {
60+
if (!enabled) {
61+
return;
62+
}
5963

60-
const id = setInterval(preservedCallback, delay);
61-
return () => clearInterval(id);
62-
}, [delay, preservedCallback, enabled]);
64+
const id = setInterval(preservedCallback, delay);
65+
return () => clearInterval(id);
66+
},
67+
[delay, preservedCallback, enabled]
68+
);
6369
}

packages/core/src/hooks/useLoading/useLoading.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,15 @@ export function useLoading(): [boolean, <T>(promise: Promise<T>) => Promise<T>]
5858
function useIsMountedRef() {
5959
const ref = useRef({ isMounted: true }).current;
6060

61-
useEffect(() => {
62-
ref.isMounted = true;
63-
return () => {
64-
ref.isMounted = false;
65-
};
66-
}, [ref]);
61+
useEffect(
62+
function trackMountedState() {
63+
ref.isMounted = true;
64+
return () => {
65+
ref.isMounted = false;
66+
};
67+
},
68+
[ref]
69+
);
6770

6871
return ref;
6972
}

packages/core/src/hooks/usePreservedCallback/usePreservedCallback.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ export function usePreservedCallback<Arguments extends any[] = any[], ReturnValu
3232
) {
3333
const callbackRef = useRef(callback);
3434

35-
useEffect(() => {
36-
callbackRef.current = callback;
37-
}, [callback]);
35+
useEffect(
36+
function syncCallbackRef() {
37+
callbackRef.current = callback;
38+
},
39+
[callback]
40+
);
3841

3942
return useCallback((...args: Arguments) => {
4043
return callbackRef.current(...args);

0 commit comments

Comments
 (0)