Skip to content
Troubleshooting⏱️ 9 min read·

React “Objects are not valid as a React child”: What It Means and How to Fix It

Fix the #1 rendering error in React and Next.js: 'Objects are not valid as a React child'. Learn why rendering raw JavaScript objects crashes your UI, how to inspect API data safely, and how to map object arrays.

MN
Manoj NeupaneSoftware Developer & Programming Instructor
#React#Next.js#JavaScript#JSX#Debugging#Error Fix
🎯 Who This Tutorial Is For

📋 Prerequisites
  • ✓A React or Next.js application fetching data from an API
  • ✓Basic understanding of JSX and JavaScript data types

🚀 What You Will Learn & Build

  • ▸Why React cannot render plain JavaScript objects inside JSX expressions
  • ▸The 3 most common scenarios that cause this error
  • ▸How to safely render object properties, array maps, and debug with JSON.stringify
1

Why React Throws This Error

In JSX, curly braces `{expression}` can only render **primitive values** (strings, numbers) or valid React Elements (like `
` or another component). React **cannot** render a plain JavaScript object `{ ... }` directly: ```jsx // THIS CRASHES REACT: const user = { name: "Manoj", email: "manoj@example.com" }; return
Welcome, {user}!
; // 💥 Error! ``` The exact error message reads: ```bash Error: Objects are not valid as a React child (found: object with keys {name, email}). If you meant to render a collection of children, use an array instead. ```
2

The 3 Solutions

### Solution 1: Access Specific Primitive Properties Render strings or numbers directly: ```jsx
Welcome, {user.name}! ({user.email})
``` ### Solution 2: Mapping Arrays of Objects When rendering lists fetched from your backend, map over the array and return JSX: ```jsx
    {products.map((item) => (
  • {item.title} — NPR {item.price}
  • ))}
``` ### Solution 3: Quick Debugging with JSON.stringify If you want to inspect a raw object on screen during development: ```jsx
{JSON.stringify(user, null, 2)}
```

✨ Expected Output & Verification

Clean, crash-free React component rendering with properly structured JSX property access.

🧪 How to Test the Implementation

  1. 1Check your console for the line number pointing to the offending JSX curly brace
  2. 2Identify the variable inside {variable} and inspect its type using typeof or console.log
  3. 3Replace with primitive property access like {variable.title}

⚠️ Common Errors & How to Fix Them

❌Passing Date object directly in JSX

Root Cause: Writing <div>{new Date()}</div> instead of <div>{new Date().toLocaleDateString()}</div>.

Fix: Format dates to strings using toLocaleDateString() or date-fns.

🛡️ Security Considerations & Best Practices

  • !Always validate that nested properties exist before accessing: {user?.profile?.bio} to prevent null pointer exceptions.

❓ Frequently Asked Questions (FAQ)

Yes, React can render arrays of strings, numbers, or JSX elements (like [<span>A</span>, <span>B</span>]). It only rejects arrays containing raw object literals.

📌 Summary of Key Takeaways

React cannot render raw object literals inside JSX. Always extract primitive strings or numbers, map over arrays with keys, or use JSON.stringify for debugging.

Official References & Further Reading