Skip to content

Commit dd526d0

Browse files
committed
fix: guard pending delay handles
1 parent 25060d9 commit dd526d0

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

src/hooks/useDelayState.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@ export default function useDelayState<T>(
1919
defaultValue: T | (() => T),
2020
): [T, SetDelayState<T>] {
2121
const [value, setValue] = React.useState(defaultValue);
22-
const rafRef = React.useRef<number>(null);
23-
const timeoutRef = React.useRef<ReturnType<typeof setTimeout>>(null);
22+
const rafRef = React.useRef<number | null>(null);
23+
const timeoutRef = React.useRef<ReturnType<typeof setTimeout> | null>(null);
2424

2525
const cancelPending = useEvent(() => {
26-
raf.cancel(rafRef.current!);
27-
clearTimeout(timeoutRef.current!);
28-
rafRef.current = null;
29-
timeoutRef.current = null;
26+
if (rafRef.current !== null) {
27+
raf.cancel(rafRef.current);
28+
rafRef.current = null;
29+
}
30+
if (timeoutRef.current !== null) {
31+
clearTimeout(timeoutRef.current);
32+
timeoutRef.current = null;
33+
}
3034
});
3135

3236
const setDelayValue = useEvent<SetDelayState<T>>(

0 commit comments

Comments
 (0)