If you've been working with React for a while, you may have encountered the "Error: Objects are not valid as a React child (found: [object Promise])" error message. This error can be frustrating to deal with, especially if you're not sure what's causing it.
In this article, we'll explain what the error message means, common causes, and how to fix it.
Understanding the Error
The "Error: Objects are not valid as a React child (found: [object Promise])" error message occurs when you try to render a Promise object as a child in a React component. React expects you to render a plain JavaScript object or an array of objects, not a Promise object.
Common Causes
There are a few common causes of this error, including:
Trying to render a Promise object as a child in a React component.
Forgetting to resolve the Promise before rendering it in a React component.
Using the async keyword in a function that's used to render a React component.
Solution
To fix the error, you need to ensure that you're rendering a plain JavaScript object or an array of objects, not a Promise object. You can do this by resolving the Promise before rendering it in your React component. Here's an example of how you might do this:
import React, { useState, useEffect } from 'react';
function FetchListting() {
const [Listing, setListing] = useState([]);
useEffect(() => {
const fetchData = async () => {
const response = await fetch('https://example.com/data');
const json = await response.json();
setListing(json);
};
fetchData();
}, []);
return (
<div>
{list. Map((item) => (
<div key={item.id}>{item.name}</div>
))}
</div>
);
}
In this example, we're using the useEffect hook to fetch Listing-data from an API and resolve the Promise before setting the data to the component's state using the setData function. The component then maps over the List array and renders it as a list of
elements.Another Solution
Removing the async keyword in a function that's used to render a React component.
Here is a code example that can result to an error,
async function FetchListting() {
const [Listing, setListing] = useState([]);
useEffect(() => {
const fetchData = async () => {
const response = await fetch('https://example.com/data');
const json = await response.json();
setListing(json);
};
fetchData();
}, []);
return (
<div>
{list. Map((item) => (
<div key={item.id}>{item.name}</div>
))}
</div>
);
}
Removing the async keyword would resolve the error
function FetchListting() {
const [Listing, setListing] = useState([]);
useEffect(() => {
const fetchData = async () => {
const response = await fetch('https://example.com/data');
const json = await response.json();
setListing(json);
};
fetchData();
}, []);
return (
<div>
{list. Map((item) => (
<div key={item.id}>{item.name}</div>
))}
</div>
);
}
Conclusion
The "Error: Objects are not valid as a React child (found: [object Promise])" error can be challenging to deal with, but by resolving the Promise before rendering it in your React component, you can fix it. Make sure to keep this solution in mind the next time you encounter this error in your React application.
please if you enjoy reading this article, subscribe to my YouTube channel MY Channel
please if you enjoy reading this article, follow me on Twitter
Top comments (2)
Thank you!!!
Thank you for your article
But this is not working for me.