import React from 'react'; import { useEffect, useRef, useState } from 'react'; function Home() { const [index, setIndex] = useState(0); const colors = ['#FF0000', '#00FF00', '#0000FF']; const timeout = useRef(0); useEffect(() => { resetTimeout(); timeout.current = window.setTimeout( () => setIndex((prevIndex) => prevIndex === colors.length - 1 ? 0 : prevIndex + 1 ), 2500 ); return () => { resetTimeout(); }; }, [index]); const resetTimeout = () => { if (timeout.current) { clearTimeout(timeout.current); } }; return (
{colors.map((backgroundColor, colorIndex) => (
))}
); } export default Home;