sendou.ink/app/hooks/useDebounce.ts
Kalle 9e829614ed
Some checks are pending
E2E Tests / e2e (push) Waiting to run
Tests and checks on push / run-checks-and-tests (push) Waiting to run
Updates translation progress / update-translation-progress-issue (push) Waiting to run
Replace react-use dependency with a few helpers
2026-06-12 22:40:50 +03:00

20 lines
498 B
TypeScript

import * as React from "react";
/**
* Runs `fn` once `ms` has elapsed without any value in `deps` changing. The
* timer is (re)started on mount and whenever `ms` or a value in `deps` changes.
*/
export function useDebounce(
fn: () => void,
ms = 0,
deps: React.DependencyList = [],
) {
const callback = React.useRef(fn);
callback.current = fn;
React.useEffect(() => {
const timeout = setTimeout(() => callback.current(), ms);
return () => clearTimeout(timeout);
}, [ms, ...deps]);
}