Skip to content

Commit

Permalink
[BM-296] ✨ useInterval 훅 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
sookyeonghwang committed Aug 10, 2022
1 parent d2b48df commit b7b298b
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions hooks/useInterval.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useEffect, useRef } from 'react';

const useInterval = (callback: any, delay: number) => {
const savedCallback = useRef<any>();

useEffect(() => {
savedCallback.current = callback;
}, [callback]);

useEffect(() => {
const tick = () => {
savedCallback.current();
};

if (delay !== null) {
const id = setInterval(tick, delay);
return () => clearInterval(id);
}
}, [delay]);
};

export default useInterval;

0 comments on commit b7b298b

Please sign in to comment.