init
This commit is contained in:
20
src/clientStore/useInterval.ts
Normal file
20
src/clientStore/useInterval.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
'use client';
|
||||
import { useEffect, useRef } from 'react';
|
||||
|
||||
// https://overreacted.io/making-setinterval-declarative-with-react-hooks/
|
||||
export default function useInterval(callback: () => void, delay: number | undefined) {
|
||||
const savedCallback = useRef<typeof callback>();
|
||||
|
||||
useEffect(() => {
|
||||
savedCallback.current = callback;
|
||||
}, [callback]);
|
||||
|
||||
useEffect(() => {
|
||||
const handler = () => savedCallback.current?.();
|
||||
|
||||
if (delay !== null) {
|
||||
const id = setInterval(handler, delay);
|
||||
return () => clearInterval(id);
|
||||
}
|
||||
}, [delay]);
|
||||
}
|
||||
Reference in New Issue
Block a user