Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why map() function doesn't work in my react app?

So I have a usual container which displays some products, and I get products data(like img, title) from the file called data.js, and map through it to display it.

It looks like this:

<div className="wrapper">
                {sliderItems.map((item)=>{
                    <div className="slide">
                    <div className="img_container">
                        <img src={item.img}/>
                    </div>
                    <div className="info_container">
                        <h1>{item.title}</h1>
                        <p>{item.desc}</p>
                        <button>Button</button>
                    </div>
                </div>
                })}
            </div>

And my data.js looks like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import beats from './assets/beats.png';
import headphone from './assets/headphone.png';
import girl from './assets/girl.png';

export const sliderItems = [
    {
      id: 1,
      img: beats,
      title: "SUMMER SALE",
      desc: "DON'T COMPROMISE ON STYLE! GET FLAT 30% OFF FOR NEW ARRIVALS.",
      bg: "f5fafd",
    },
    {
      id: 2,
      img: headphone,
      title: "AUTUMN COLLECTION",
      desc: "DON'T COMPROMISE ON STYLE! GET FLAT 30% OFF FOR NEW ARRIVALS.",
      bg: "fcf1ed",
    },
    {
      id: 3,
      img: girl,
      title: "LOUNGEWEAR LOVE",
      desc: "DON'T COMPROMISE ON STYLE! GET FLAT 30% OFF FOR NEW ARRIVALS.",
      bg: "fbf0f4",
    },
  ];

But for some reason, it doesn’t display in container, like it’s totally empty on the page. I looked up in the browser code, and ‘wrapper’ div is just empty:

enter image description here

So I’m wondering what could be the reason that map function doesn’t work.

>Solution :

your not returning from the map() function. This should solve it:

             <div className="wrapper">
                    {sliderItems.map((item)=> ( <div className="slide">
                        <div className="img_container">
                            <img src={item.img}/>
                        </div>
                        <div className="info_container">
                            <h1>{item.title}</h1>
                            <p>{item.desc}</p>
                            <button>Button</button>
                        </div>
                    </div>)
                    )}
                </div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading