I'm trying to create a slideshow on my React app using React Hooks but it doesn't seem to be working correctly. My code is as follows:
let i = 0;
const slides = [Slide1, Slide2, Slide3, Slide4]
const [slide, setSlide] = useState(slides[i]);
setInterval(() => {
if ( i < slides.length - 1) {
i++;
} else {
i = 0;
}
setSlide(slides[i])
}, 1000);
The slideshow starts at slide one and goes to slide two correctly and after that it 'glitches' and either doesn't move onto slide three or quickly alternates between slide two and three.
I have also tried this function which produces the same result:
const changeSlide = () => {
if ( i < slides.length - 1) {
i++;
} else {
i = 0;
}
setSlide(slides[i])
};
setInterval(() => changeSlide(), 1000);
I have tried the program with 3, 4 and 5 slides all producing the same result.