I'm trying to make a loop with a few images using React.
I made this logical, and for me, it should work, but it doesn't! I did the same logical for one html page with javascript, but in a React Component it isn't working.
export default function AnimatedImage() {
const [imageRoute, setImageRoute] = useState('source1')
const [frame, setFrame] = useState(1)
const imagesPath = [
"source1",
"source2",
"source3",
"source4"
]
function handleAnimation(){
setImageRoute(imagesPath[frame])
if (frame === 3)
setFrame(0)
setFrame(frame + 1)
}
setInterval(handleAnimation, 300);
return (
<img src={imageRoute} />
)
}
I'm having some issues with the image itself, sometimes it disappear showing the icon of "no image found", and in a few milliseconds, it appear again. And sometimes the interval get crazy and start to loop really fast ignoring the time set.
This only happens here in React.
Nothing make sense here Someone can help?