Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

211
Views
How to change image every second in map react

Im trying to change my image every second in react inside of a map. However this doesnt seem to be working. Here is my code:

function ImageGrid() {
const {shell, gridImage} = styles
const squishAnimals = [
    '/SquishAnimalDemo(1).png',
    '/SquishAnimalDemo(2).png',
    '/SquishAnimalDemo(3).png',
    '/SquishAnimalDemo(4).png',
    '/SquishAnimalDemo(5).png',
    '/SquishAnimalDemo(6).png',
    '/SquishAnimalDemo(7).png',
    '/SquishAnimalDemo(8).png',
    '/SquishAnimalDemo(9).png'
];
const [images, setImages] = useState(squishAnimals);
const [currentImage, setCurrentImage] = useState(0);

function changeBackgroundImage() {
    let newCurrentImg = 0;
    const noOfImages = images.length;

    if (currentImage !== noOfImages - 1) {
        newCurrentImg += currentImage;
    }

    setCurrentImage(newCurrentImg);
}

useEffect(() => {
    const interval = setInterval(() => changeBackgroundImage(), 1000);

    if (interval) {
        clearInterval(interval);
    }

});

return (
    <div className={shell}>
        {squishAnimals.map((image, index) => {
            return (
                index === 4 ?
                    <img className={gridImage} src={images[currentImage]} alt={`${index}-${image}`}/> :
                    <img className={gridImage} src={image} alt={`${index}-${image}`}/>
            )
        })}
    </div>
);

}

Any help as to why it may not be working would be great. Thanks :)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think it's because you immediately cancel the interval in useEffect. I believe you want to return a cleanup function to clear the interval id.

function ImageGrid() {
const {shell, gridImage} = styles
const squishAnimals = [
    '/SquishAnimalDemo(1).png',
    '/SquishAnimalDemo(2).png',
    '/SquishAnimalDemo(3).png',
    '/SquishAnimalDemo(4).png',
    '/SquishAnimalDemo(5).png',
    '/SquishAnimalDemo(6).png',
    '/SquishAnimalDemo(7).png',
    '/SquishAnimalDemo(8).png',
    '/SquishAnimalDemo(9).png'
];
const [images, setImages] = useState(squishAnimals);
const [currentImage, setCurrentImage] = useState(0);

function changeBackgroundImage() {
    let newCurrentImg = 0;
    const noOfImages = images.length;

    if (currentImage !== noOfImages - 1) {
        newCurrentImg += currentImage;
    }

    setCurrentImage(newCurrentImg);
}

useEffect(() => {
    const interval = setInterval(() => changeBackgroundImage(), 1000);

    return () => { 
       if (interval) {
          clearInterval(interval);
       }
    }
});

return (
    <div className={shell}>
        {squishAnimals.map((image, index) => {
            return (
                index === 4 ?
                    <img className={gridImage} src={images[currentImage]} alt={`${index}-${image}`} key={images[currentImage]}/> :
                    <img className={gridImage} src={image} alt={`${index}-${image}`} key = {`${index}-${image}`}/>
            )
        })}
    </div>
 );
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!