It is necessary to get values from an array of objects and display them on the screen. Now I get an error in the console: Error: Objects are not valid as a React child (found: object with keys {title}). If you meant to render a collection of children, use an array instead. It also says in the console: The above error occurred in the component. How can this problem be solved?
ListItem.js:
const ListItem = (props) => {
return (
<div>
<h2>{props}</h2>
</div>
);
};
export default ListItem;
List.js:
import ListItem from "./ListItem/ListItem";
const listObj = [
{
title: "House 1",
},
{
title: "House 2",
},
{
title: "House 3",
},
];
function HouseList() {
return listObj.map((house, index) => {
return <ListItem key={index} title={house.title} />;
});
}
const List = () => {
return (
<div style={{ width: "50%" }}>
<HouseList />
</div>
);
};
export default List;
props.titleinstead of justpropsin theh2element.props.titleinstead ofpropsinListItemJSX.ListItemhas a single prop (apart fromkey), so you could simply do:<h2>{props.title}</h2>. Anyway, if you have to display all the props and none of your props values is an object, you simply do the same thing you have done forHouseList:{Object.values(props).map(prop => <h2>prop</h2>)}