props are passing fine when we are passing them as a whole array of objects but it is not working when I am passing the props by traversing through the array using map function.
JavaScript
x
21
21
1
import { React, useEffect, useState } from "react";
2
3
import axios from "axios";
4
5
import "./Home.css";
6
import Cardimg from "./Cardimg";
7
const Home = props => {
8
return (
9
<>
10
<div className="header">PHOTO GALLERY</div>
11
<div className="photos">
12
{props.data?.map(e => {
13
<Cardimg data={e.ImgUrl}></Cardimg>;
14
})}
15
</div>
16
</>
17
);
18
};
19
20
export default Home;
21
in the above code props are passing when I am passing manually in Cardimg component…but as soon as I start using map then it doesn’t work…like the props are not reaching the component.
below is my Cardimg component
JavaScript
1
11
11
1
import React from 'react'
2
3
const Cardimg = (props) => {
4
console.log(props.data);
5
return (
6
<div>{props.data}</div>
7
)
8
}
9
10
export default Cardimg
11
Advertisement
Answer
You need to return the Cardimg
component inside map
callback function.
Either like this
JavaScript
1
6
1
{
2
props.data?.map(e => {
3
return <Cardimg data={e.ImgUrl}></Cardimg>;
4
});
5
}
6
Or like this
JavaScript
1
4
1
{
2
props.data?.map(e => <Cardimg data={e.ImgUrl}></Cardimg>)
3
}
4