Skip to content

Commit 86da92f

Browse files
committed
fix: trigger UI update when cancel() is called
Ensures isPending() state correctly updates in the UI when canceling an active debounce timer
1 parent f1c7511 commit 86da92f

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/useDebouncedCallback.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ export default function useDebouncedCallback<
286286
};
287287

288288
func.cancel = () => {
289+
const hadTimer = !!timerId.current;
289290
if (timerId.current) {
290291
useRAF
291292
? cancelAnimationFrame(timerId.current)
@@ -297,6 +298,11 @@ export default function useDebouncedCallback<
297298
lastThis.current =
298299
timerId.current =
299300
null;
301+
302+
// Notify React to re-render when cancel is called and there was an active timer
303+
if (hadTimer && forceUpdate) {
304+
forceUpdate({});
305+
}
300306
};
301307

302308
func.isPending = () => {

0 commit comments

Comments
 (0)