Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

React throws 'Objects are not valid as a React child'

ErrorObjects are not valid as a React child (found: object with keys {id, name})

Short answer

React can render elements, strings, numbers, empty values and arrays of those. A plain object, or an array of plain objects, is none of these, so JSX that places one between braces fails. Find the value named in the message, then render its fields, map it to elements, or read a Promise with use().

  • Applies to React 19, TypeScript 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the keys the message prints. They identify the object. Then log the value that you put in braces.

    console.log(typeof value, Array.isArray(value), value);
  2. 2Match what you see, then jump to its fix

    1. 1The message lists the object's keys, for example {street, city}A whole object (often a nested one) is put in JSXFix
    2. 2The value is an array whose items are objectsAn array of objects is placed in JSX without mappingFix
    3. 3The value comes from fetch() or an async functionA Promise is rendered instead of its resultFix

What does the error look like?

  • Objects are not valid as a React child (found: object with keys {id, name})
  • If you meant to render a collection of children, use an array instead.

Cause 1 of 3

A whole object (often a nested one) is put in JSX

1Check

React accepts elements, strings, numbers, portals, null, undefined, booleans and arrays of nodes as children. An object is not on that list.

grep -rn "{user\.\|{data}" src/ | head

2Fix

Render the fields you want, one by one.

// Fails
<p>{user.address}</p>

// Works
<p>{user.address.street}, {user.address.city}</p>

3Verify

The page renders and the message is gone.

# reload the page

Confirmed by react.dev

Did this fix it?

Cause 2 of 3

An array of objects is placed in JSX without mapping

1Check

An array is fine only when its items are valid React nodes. Objects first have to be turned into elements.

console.log(Array.isArray(items), items[0]);

2Fix

Map each object to an element with a unique key.

<ul>
  {items.map(item => (
    <li key={item.id}>{item.name}</li>
  ))}
</ul>

3Verify

One list item appears for each object.

# reload the page

Confirmed by react.dev

Did this fix it?

Cause 3 of 3

A Promise is rendered instead of its result

1Check

A Promise is an object. Rendering it directly fails, and an async function used as a component returns one.

console.log(value instanceof Promise);

2Fix

Read the Promise with use() under a Suspense boundary. The Promise must be created outside render, for example in a Server Component or a cache, so the same instance is reused.

import { use, Suspense } from 'react';

function Albums({ albumsPromise }) {
  const albums = use(albumsPromise);
  return <ul>{albums.map(a => <li key={a.id}>{a.title}</li>)}</ul>;
}

<Suspense fallback={<h2>Loading...</h2>}>
  <Albums albumsPromise={albumsPromise} />
</Suspense>

3Verify

The fallback shows while loading and the list replaces it.

# reload the page

Confirmed by react.dev

Did this fix it?

How do I stop it coming back?

  • Type API responses so a field that is an object cannot be placed in JSX by accident.
  • Use JSON.stringify(value) only for debugging output, never as the final UI.
  • Keep data fetching out of render, and pass Promises down from where they are created.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

TypeScript rejects MUI Data Grid columns that set a type

A TypeScript error inside a column's renderCell callback

A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.