I have a parent component called ListContainer which contains a child component called ListItem. I need ListItem to be called for every item in an array called nameArray, and for the ListItem's name to be populated from the values in this array.
export const ListItem = (props) => {
return <h2>Name: { props.name }</h2>;
};
export const ListContainer = (props) => {
const nameArray = [
{
name: "john",
age: "29"
},
{
name: "james",
age: "21"
}
];
return (
<div>
{ this.nameArray.map(function(item) {
return (
<ListItem name={ item.name } />
)
}) }
</div>
);
};
From looking at the docs I think I need to use the map function, but im confused as to how it works in JSX.