I am using a reusable component for React, and I am trying to map over an array within an object of arrays to go inside of this component.
the data looks like:
export const portfolioData=
[{
title: "Drum Kit",
url: "some url",
codeUrl: "some github repo",
mobileFriendly: false,
img: "some img",
techUsed: [
"React",
"APIs",
"Custom Hooks",
],
},
The map looks like:
<div>
{portfolioData.map((portfolio,i)=>(<PortfolioCard key={i}>{portfolio}</PortfolioCard> ))}
</div>
and the reusable component looks like:
<div>
<img src={img} />
<h3>{title}</h3>
<button target="_blank" href={url}><FaCode />
</button>
<button target="_blank" href={codeUrl}><CgWebsite />
</button>
<ul>
<li>{mobileFriendly}</li>
<li>{techUsed}</li>
</ul>
</div>
When I use .map() it recognises that I have mapped over something but it doesn’t show anything up on the screen, just three empty components (I have 3 objects in my array).
3
Answers
I’m just learning React (in fact did it over the Thanksgiving holiday!) and this was one of the first things I learned.
The issue with your map is that the output produces a
PortfolioCard
component for each item in Array. Because of that, you have to enclose it in either a<div>
tag or a<>
tag.I whipped up an example using your code that you can try out.
(I can’t figure out how the StackOverflow in-page console works, sorry).
If you want to use the property values in the object as props, you can spread them into the component.
Do the following:
I modified your array as it was missing this
]
Next, Mapped
portfolioData
with thePortfolioCard
component while passing theportfolioData
object as a prop toPortfolioCard
After that, modify your
PortfolioCard
component to take in a propportfolio
and use it through out the entire component below: