I am setting up a React app with a Rails backend. I am getting the error \"Objects are not valid as a React child (found: object with keys {id, name, info, created_at, updat
I got the same error today but with a different scenario as compared to the scenario posted in this question. Hope the solution to below scenario helps someone.
The render
function below is sufficient to understand my scenario and solution:
render() {
let orderDetails = null;
if(this.props.loading){
orderDetails = <Spinner />;
}
if(this.props.orders.length == 0){
orderDetails = null;
}
orderDetails = (
<div>
{
this.props.orders.map(order => (
<Order
key={order.id}
ingredient={order.ingredients}
price={order.price} />
))
}
</div>
);
return orderDetails;
}
In above code snippet : If return orderDetails
is sent as return {orderDetails}
then the error posted in this question pops up despite the value of 'orderDetails' (value as <Spinner/>
or null
or JSX related to <Order />
component).
Error description : react-dom.development.js:57 Uncaught Invariant Violation: Objects are not valid as a React child (found: object with keys {orderDetails}). If you meant to render a collection of children, use an array instead.
We cannot return a JavaScript object from a return call inside the render() method. The reason being React expects a component or some JSX or null to render in the UI and not some JavaScript object that I am trying to render when I use return {orderDetails}
and hence get the error as above.
In JavaScript, arrays and collections are different, although they are somewhat similar, but here the react needs an array.
You need to create an array
from the collection
and apply it.
let homeArray = new Array(homes.length);
let i = 0
for (var key in homes) {
homeArray[i] = homes[key];
i = i + 1;
}
I had a similar error while I was creating a custom modal.
const CustomModal = (visible, modalText, modalHeader) => {}
Problem was that I didn't wrap my values to curly brackets like this.
const CustomModal = ({visible, modalText, modalHeader}) => {}
If you have multiple values to pass to the component, you should use curly brackets around it.
Had the same issue, In my case I had 1. Parse the string into Json 2. Ensure that when I render my view does not try to display the whole object, but object.value
data = [
{
"id": 1,
"name": "Home Page",
"info": "This little bit of info is being loaded from a Rails
API.",
"created_at": "2018-09-18T16:39:22.184Z",
"updated_at": "2018-09-18T16:39:22.184Z"
}];
var jsonData = JSON.parse(data)
Then my view
return (
<View style={styles.container}>
<FlatList
data={jsonData}
renderItem={({ item }) => <Item title={item.name} />}
keyExtractor={item => item.id}
/>
</View>);
Because I'm using an array, I used flat list to display, and ensured I work with object.value, not object otherwise you'll get the same issue
I faced same issue but now i am happy to resolve this issue.
npm i core-js
index.js
file.
import core-js
Well in my case the data which I wanted to render contained an Object inside that of the array so due to this it was giving error, so for other people out there please check your data also once and if it contains an object, you need to convert it to array to print all of its values or if you need a specific value then use.
My data :
body: " d fvsdv"
photo: "http://res.cloudinary.com/imvr7/image/upload/v1591563988/hhanfhiyalwnv231oweg.png"
postedby: {_id: "5edbf948cdfafc4e38e74081", name: "vit"} //this is the object I am talking about.
title: "c sx "
__v: 0
_id: "5edd56d7e64a9e58acfd499f"
proto: Object
To Print only a single value
<h5>{item.postedby.name}</h5>