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