mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
20 lines
478 B
TypeScript
20 lines
478 B
TypeScript
import { useRef, useEffect } from 'react';
|
|
|
|
const useInterval = (callback: Function, delay?: number | null) => {
|
|
const latestCallback = useRef<Function>(() => {});
|
|
|
|
useEffect(() => {
|
|
latestCallback.current = callback;
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (delay !== null) {
|
|
const interval = setInterval(() => latestCallback.current(), delay || 0);
|
|
return () => clearInterval(interval);
|
|
}
|
|
return undefined;
|
|
}, [delay]);
|
|
};
|
|
|
|
export default useInterval;
|