useTimeout
A custom hook to conveniently use window.setTimeout
.
You can stop the timer by putting null
as the value of delay
.
Interface
type SetTimeoutParameters = Parameters<typeof setTimeout>;
const useTimeout: (callback: SetTimeoutParameters[0], delay?: SetTimeoutParameters[1]) => void
Usage
import { useTimeout } from '@devgrace/react';
const Example = () => {
const [number, setNumber] = useState(0);
useTimeout(() => {
setNumber(number + 1);
}, 1000);
useTimeout(() => {
setNumber(number + 1);
}, 2000);
return <div>{number}</div>;
};