I am trying to iterate over a JSON data set which i am sending as a prop. I tried printing the props and i see that the data is there. However i cannot seem to use .map as it states that it is not a property of the prop data.
Parent Component:
function HomePage() {
return (
<React.StrictMode>
<VolvoCarsProducstListPage cd={cars} />
</React.StrictMode>
);
}
export default HomePage;
Child component:
import React, { useState } from 'react';
export interface CarDataProps {
cars: Car[];
};
export interface Car {
id: string,
modelName: string,
bodyType: string,
modelType: string,
imageUrl: string
}
export const VolvoCarsProducstListPage = (props: CarDataProps) => {
const carsJSONData = props;
carsJSONData.map()
console.log(carsJSONData);
return (
<div className="volvo-product-list-page">
</div>
);
};
2
Answers
It looks like you’re trying to call
.map
on an object containing cars, not the array with cars. if you change these two lines it should workto
Property map does not exist because
carsJSONData
is not an array it’s theprops
the component recieves and this latter is of typeCarDataProps
which is an object that contains an array so you cannot map through it.however you can map through
carsJSONData.cars
or simply makecarsJSONData = props.cars
instead, if you wantcarsJSONData
to be the propertycars
of the received props which is an array and pass props to the component this way :to avoid any conflict