1 import { useRef, useEffer } from "react"; 2 export function useInterval(callback, delay) { 3 const savedCallback = useRef(); 4 5 useEffect(() => { 6 savedCallback.current = callback; 7 }); 8 useEffect(() => { 9 function tick() { 10 savedCallback.current(); 11 } 12 if (delay !== null) { 13 let id = setInterval(tick, delay); 14 return () => clearInterval(id); 15 } 16 }, [delay]); 17 return () => clearInterval(id); 18 }, [delay]);
标签:savedCallback,useRef,setInterval,delay,获取,useState,id From: https://www.cnblogs.com/Dluea/p/17187511.html