useRafInterval
function useRafInterval(callback: () => void, ms: number | null): void;
useInterval timed with animation frames: calls callback on the first frame
each time ms has passed since the last call, or since it started. Browsers pause frames while
the tab is hidden, so it stops in the background, and when the tab is shown again it calls once
instead of catching up on every missed call. That suits animation and other visual work. Pass
null to pause; 0, a negative, NaN or an infinite ms also pauses. Resuming or changing
ms counts from zero again. The latest callback is always called without restarting, so it
need not be memoised. Nothing runs on the server.
Live demo
🌅 Sunrise
Slide 1 of 4
Example
import { useState } from "react";
import { useRafInterval } from "react-honey-hooks";
function Slideshow({ slides }: { slides: string[] }) {
const [index, setIndex] = useState(0);
useRafInterval(() => {
setIndex((current) => (current + 1) % slides.length);
}, 5000);
return <img src={slides[index]} alt="" />;
}