I'm trying to make a slideshow of 5 pictures using react hooks and a setInterval.
So when the counter comes to photo 5(index 4) it goes back to photo1(index 0).
The counter seems ok, but keeps counting when it comes to 4 even though the condition in the if statement is false.
import React, { useState, useEffect } from 'react';
import { photoSource } from './photo-source.js';
function Slideshow() {
const [imgSource, setImgSource] = useState(0);
useEffect(() => {
const intervalId = setInterval(() => {
if (imgSource <= 1) {
setImgSource(imgSource => imgSource + 1);
} else {
setImgSource(0);
}
}, 10000);
return () => clearInterval(intervalId);
}, []);
console.log('ImgSource: ' + imgSource);
console.log(imgSource <= 1)
console.log(photoSource[imgSource].src)
All the values logged to the console seems right, the only obvious problem is that the counter keeps counting as if the if statement would not work.