Fix Uncaught Error Objects are not valid as a React child
By Flavio
Published · Updated
Tagged: React
Fix React's Objects are not valid as a React child error: forgotten prop destructuring, double curly braces, Date objects, arrays of objects and async components.
This is the full error you see in the browser console:
Uncaught Error: Objects are not valid as a React child (found: object with keys {test}). If you meant to render a collection of children, use an array instead.
React can render strings, numbers, elements and arrays of those. null, undefined, true and false render nothing. A plain object is none of these, so React doesn’t know what to show and throws.
The part in parentheses is the most useful clue. object with keys {test} tells you the shape of the object that ended up in your JSX, so you can search your code for it.
Here are the usual causes, and how to fix each one.
You forgot to destructure the props
This error might happen because you’ve got a React component and you’re rendering a prop in the JSX, but you forgot to destructure the prop when initializing the component parameters:
function MyComponent(test) {
return <p>{test}</p>
}
<MyComponent test="hello" />
You should do this instead:
function MyComponent({ test }) {
return <p>{test}</p>
}
The component receives a props object and we commonly use object destructuring to get the props “mapped” to variables, like {test} in our example. Without the curly braces, test is the whole props object { test: 'hello' }, and that’s exactly what the error reports: object with keys {test}.
You rendered an object instead of one of its properties
The same error appears whenever JSX receives a plain object:
const user = { name: 'Flavio' }
return <p>{user}</p> // error
Render a property instead:
return <p>{user.name}</p>
This often happens with data from an API. You expect a string and the API returns an object like { id: 1, name: 'Flavio' }. Log the value with console.log() to see what you actually got.
You used double curly braces
Coming from templating languages like Handlebars or Vue, it’s easy to write this:
const name = 'Flavio'
return <p>{{name}}</p> // error
In JSX, the outer braces start a JavaScript expression, and the inner ones create an object. {{name}} is the object { name: name }, so you get object with keys {name}. Use one pair of braces:
return <p>{name}</p>
Double braces are correct in one place: the style prop, as in style={{ color: 'red' }}. There the object is passed to a prop, not rendered as a child.
You rendered a Date
A Date is an object too. The error message looks slightly different:
Objects are not valid as a React child (found: [object Date]).
Convert it to a string first:
const createdAt = new Date()
return <p>{createdAt.toLocaleDateString()}</p>
toLocaleDateString(), toLocaleString() and toISOString() all return strings. If you need a specific format, Intl.DateTimeFormat gives you full control.
You rendered an array of objects
An array of strings works, an array of objects doesn’t:
const users = [
{ id: 1, name: 'Flavio' },
{ id: 2, name: 'Roger' }
]
return <ul>{users}</ul> // error: object with keys {id, name}
Use map() to turn each object into an element, with a stable key:
return (
<ul>
{users.map((user) => (
<li key={user.id}>{user.name}</li>
))}
</ul>
)
That’s what “If you meant to render a collection of children, use an array instead” means: an array of elements, not of objects. I wrote more about this in how to loop inside React JSX.
You made a client component async
An async function always returns a promise. If you write a component like this in a regular React app (not a Server Component):
async function Profile() {
const res = await fetch('/api/user')
const user = await res.json()
return <p>{user.name}</p>
}
In React 18 you get Objects are not valid as a React child (found: [object Promise]), because the component returns a promise instead of JSX.
In React 19 the error is different:
An unknown Component is an async Client Component. Only Server Components can be async at the moment.
Either way, the fix is the same: keep the component synchronous and load the data in useEffect() or with a data-fetching library, or make it a Server Component if your framework supports them.
In production you see “Minified React error #31”
Production builds of React don’t include the full message. You get this instead:
Minified React error #31; visit https://react.dev/errors/31?args[]=object%20with%20keys%20%7Bname%7D for the full message
Error #31 is this same error. Open the link to read the full message, or decode the args[] part yourself: object%20with%20keys%20%7Bname%7D is object with keys {name}. Run the app in development mode to get the full message and a component stack.
Showing an object while debugging
When you want to see what’s inside an object, turn it into a string:
<pre>{JSON.stringify(user, null, 2)}</pre>
This is useful while debugging, but it’s rarely the right user interface.
Want me to talk about your product? You can sponsor this site.