0

Is there any problem of using array to save src in it to display on component, it doesn't work?

    const imgs = [
      './../imgs/book1.jpg',
      './../imgs/book2.jpg',
      './../imgs/book3.jpg',
    ]
    const displayImg = imgBooks.forEach(img => {
      return (
           <figure>
            <img src={img} alt='book' />
          </figure>**strong text**
      )
    })

2
  • 1
    Shouldn't you be calling foreach() on the imgs array instead of the imgBooks array since the imgs array has the images in it. Otherwise it would be helpful to see the code where the variable imgBooks is created. Commented Jun 19, 2020 at 22:34
  • 1
    forEach will not return an array so you want to use map. stackoverflow.com/a/34426481/1370487 Commented Jun 20, 2020 at 0:20

4 Answers 4

2

You need to make sure that you transform your imgs array to another array that includes your JSX markup. From there, you need to iterate over your markup array for React to render each index of displayImg array.

const Component = () => {

    const imgs = [
        './../imgs/book1.jpg',
        './../imgs/book2.jpg',
        './../imgs/book3.jpg',
    ];

    const displayImg = imgs.map(img => {
        return (
            <figure>
            <img src={img} alt='book' />
            </figure>**strong text**
        )
    });


    return displayImg.map(img => img);

}
Sign up to request clarification or add additional context in comments.

Comments

2

The forEach() function for arrays returns undefined.

If you want to show several images at the same time, choose function that return array of components: map()

const displayImgs = imgBooks.map(img => {

If your component contain only components of the array, then you can simple return the array:

const imgBooks = [
  './../imgs/book1.jpg',
  './../imgs/book2.jpg',
  './../imgs/book3.jpg',
]
return imgBooks.map(img => (<figure><img src={img} alt='book' /></figure>));

Note the name of the array: imgBooks. There was imgs vs imgBooks typo(?) in the question.

3 Comments

thanks, I really appreciate that it is helpful but the problem still there
imgs will be transformed into an array of your JSX elements which React still needs to render by using .map in your render function or your functional component return.
Added example of return.
1

If you are using create-react-app all your asset are bundled and they have different names. So, dynamic import so won't work. To overcome this you add all your images in public folder then

{ imgs.map((img) => (
   <figure>
      <img src={process.env.PUBLIC_URL + `/${data.photo}`}/>
   </figure>
))

Comments

0

This worked for me:

     {productId && images.length > 0
                ? images.map((x, index) => (
                      <img
                        src={`${backend_url}${x}`}
                        key={index}
                        alt=""
                        className="h-[120px] w-[120px] object-cover m-2"
                      />
                  ))
                 :null 
                  ))}

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.