1

I am trying to preload some images for an image carousel and store them in an array. I seem to have everything working so far except when I try to map the images in the array in to JSX I get an error.

Error: Objects are not valid as a React child (found: [object HTMLImageElement]). If you meant to render a collection of children, use an array instead

Can someone tell me why please?

As a follow up question, my setInterval (which will be used to rotate through the images) isn't starting and I can't work out why so any help with that would be greatly appreciated.

import React, { useEffect, useState } from 'react'
import { CSSTransition } from 'react-transition-group'
import { ImageCarouselContainer, ImageCarouselSlide } from './imagecarousel.styles'
const images = [
      'https://images.unsplash.com/photo-1588392382834-a891154bca4d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2555&q=80',
      'https://images.unsplash.com/photo-1441974231531-c6227db76b6e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2551&q=80',
      'https://images.unsplash.com/photo-1470813740244-df37b8c1edcb?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2551&q=80'
]

const ImageCarousel = () => {
      const [activeImage, setActiveImage] = useState(1);
      const [imagesArr, setImagesArr] = useState([])
      
      useEffect(() => {
            let loadedImages = []
            images.forEach(el => {
                  let img = new Image()
                  img.onload = () => {
                        loadedImages.push(img);
                  }
                  img.src = el
            })
            setImagesArr(loadedImages);
            const counter = () => {
                  if(activeImage < imagesArr.length) {
                        setActiveImage(activeImage + 1)
                  } else {
                        setActiveImage(0) 
                  }
            }
            
            const interval = setInterval(counter, 1000)
            
            return () => {
                  clearInterval(interval);
            }
            
      }, [])

      return (
            <ImageCarouselContainer>

                  {      
                        imagesArr &&
                        imagesArr.map((el, idx) => (
                        <CSSTransition
                              classNames='image'
                              timeout={1000}
                              key={idx}
                              in={activeImage === idx ? true : false}
                              unmountOnExit
                        >
                              <ImageCarouselSlide 
                                    
                              >
                              {el}
                              </ImageCarouselSlide>
                        </CSSTransition>  
                        ))
                  }

            </ImageCarouselContainer>
      )
}

export default ImageCarousel
4
  • Can you please put the error message? Commented Oct 22, 2020 at 11:51
  • Sorry, its: Error: Objects are not valid as a React child (found: [object HTMLImageElement]). If you meant to render a collection of children, use an array instead. Commented Oct 22, 2020 at 11:53
  • Try to wrap it on Fragment Commented Oct 22, 2020 at 11:54
  • el is an Image object. So, {el} is probably throwing an error? Commented Oct 22, 2020 at 11:58

1 Answer 1

1

you try to put pure html object not react component to render function. so it dosn't has props etc...

change

            images.forEach(el => {
                  let img = new Image()
                  img.onload = () => {
                        loadedImages.push(img);
                  }
                  img.src = el
            }

to

            images.forEach(el => {
                  let img = new Image()
                  img.onload = () => {
                        loadedImages.push(<img src={el}>); 
                  }
                  img.src = el
            }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.