I am trying to render an array of objects inside the responce object im getting back from axios.
import React, { Component } from "react";
import axios from "axios";
class Bids extends Component {
state = {
adminPosts: [],
};
componentDidMount() {
this.getPosts();
}
getPosts = async () => {
let posts = await axios.get(
"http://localhost:3001/api/posts/admin"
);
let allPosts = posts.data;
this.setState({ adminPosts: allPosts });
};
render() {
let items = [...this.state.adminPosts];
/*
This is the item array from above
[
{
_id: 1,
name: "john",
posts: [
{ _id: 1000, price: "100" },
{ _id: 1001, price: "300" },
{ _id: 1002, price: "160" },
],
},
{
_id: 2,
name: "jack",
posts: [{ _id: 1004, price: "400" }],
},
{
_id: 3,
name: "jill",
posts: [],
},
];
*/
return (
<div>
<h1>hello from Sales</h1>
{items.map((item) => (
<li key={item._id}>
<div className="container">
<p> Name: {item.name}</p>
<p> posts: {item.posts}</p> //React will not render this array of objects
</div>
</li>
))}
</div>
);
}
}
export default Bids;
I dont get any errors with {item.name} in the render method but as soon as I put in {item.posts} I get this error Error: Objects are not valid as a React child (found: object with keys { _id, price}). If you meant to render a collection of children, use an array instead.