I have an array that I’ve confirmed has content, and the keys are correct.
I’m trying to iterate through that array and put an image via the item.url
key into an Antd Card element. Below is the code I have. It compiles fine, but there is no output.
What needs to change to iterate over the array AND display the results of the Card elements thats being created?
return(<div>{this.imageDetails.forEach((item) => {
<Card hoverable cover={<img src={item.url}/>}>
<Meta title="Unoptimized Image" description="Need to optimize"/>
</Card>})}
</div>)
}
2
Answers
Use map function to return component like so
The difference between
forEach
andmap
is thatforEach
is used to iterate through an array and returnsundefined
, whilemap
can also iterate through an array, but it returns a new array (of JSX elements in this case). Withmap
, you can create dynamic length lists in React. Additionally, curly braces are used to denote code blocks, while parentheses (in React) are used to create JSX elements.