Error Encyclopedia

Objects Not Valid as a React Child Error

Fix 'Objects are not valid as a React child' error. Learn how React renders content and why objects cannot be directly rendered.

What Does This Error Mean?

This React error occurs when you try to render a JavaScript object directly in JSX. React can only render primitives (strings, numbers) and React elements. Objects must be converted to a string, mapped, or accessed by property before rendering.

Common Causes

1

Accidentally passing an object in JSX: {someObject}

2

Object stored in state rendered directly in the component

3

Array.map() missing a return statement (returns undefined for some items)

4

API response object rendered before extracting properties

5

Date object rendered without toString()

How to Fix It

Access specific properties

Render object properties individually instead of the whole object.

const user = { name: "John", email: "john@example.com" }

// ❌ Error: objects are not valid as a React child
return <div>{user}</div>

// ✅ Render specific properties
return (
  <div>
    <p>{user.name}</p>
    <p>{user.email}</p>
  </div>
)

Use JSON.stringify for debugging

Convert objects to strings for display during development.

function DebugView({ data }) {
  return (
    <pre>{JSON.stringify(data, null, 2)}</pre>
  )
}

Map arrays of objects

Use array.map() to render lists of objects.

const items = [{ id: 1, text: "First" }, { id: 2, text: "Second" }]

// ✅ Map objects to JSX
return (
  <ul>
    {items.map(item => (
      <li key={item.id}>{item.text}</li>
    ))}
  </ul>
)

Before & After Examples

❌ Before
function User({ user }) {
  return <div>{user}</div> // Error!
✅ After
function User({ user }) {
  return <div>{user.name}</div> // Works!

Related Tools

Use these tools to debug and fix this error:

Related Errors

Other common errors in this category: