Learn why passing plain objects directly into JSX crashes your React app and how to fix it cleanly.
At‑a‑Glance Fix
Convert any plain object into a renderable value before placing it in JSX, typically by:
- Accessing a specific primitive (e.g., user.name).
- Serialising the whole object (e.g., JSON.stringify(user)).
- Mapping an array of objects to an array of React elements.
Why This Error Happens
React’s renderer accepts strings, numbers, React nodes, and arrays/fragments of nodes. A bare object has no visual representation, so React throws:
Error: Objects are not valid as a React child (found: object with keys {…})
This usually happens when data from an API, state, or props is passed straight into JSX without being broken down first.
Prerequisites
- React 16+ (hooks available)
- Working knowledge of JS objects, arrays, and data fetching
- Optional: TypeScript for stronger type safety
Step‑by‑Step Guide
Follow these hands-on steps to identify, fix, and prevent this common rendering mistake in React development.
Step 1: Spot the Offending Code
function UserProfile({ user }: { user: object }) {
return (
{/* ❌ Passing entire object – will crash */}
{user}
);
}
Step 2: Render Primitive Fields or Serialise
function UserProfile({ user }: { user: User }) {
return (
{/* ✅ Render individual fields */}
Name: {user.name}
Email: {user.email}
{/* ✅ Or serialise for debugging */}
{JSON.stringify(user, null, 2)}
);}
Step 3: Handle Arrays the React Way
type User = { id: number; name: string };
const UserList: React.FC<{ users: User[] }> = ({ users }) => (
- {name}
{users.map(({ id, name }) => (
// ✅ render primitive
))}
);
Step 4: Full Working Demo
import React from “react”;
// Mock data
action const demoUser = {
id: 1,
name: “Alice Johnson”,
email: “[email protected]”,
age: 30,
address: {
street: “123 React Lane”,
city: “Devville”,
zip: “90210”,
},
};
function UserCard({ user }: { user: typeof demoUser }) {
if (!user) return
No user data.
;
return (
{user.name}
Email: {user.email}
Age: {user.age}
Address: {user.address.street}, {user.address.city}, {user.address.zip}
{/* Debug view – formatted JSON */}
{JSON.stringify(user, null, 2)}
);
}
export default function App() {
return (
React Child Error Demo
{/* Incorrect usage (commented to avoid crash) */}
{/*
*/}
);}
Highlights:
- Shows both correct and incorrect patterns.
- Inline styling avoids external CSS to keep the example self‑contained.
Common Pitfalls & How to Fix Them
| Pitfall | Example | Safe Fix |
| Rendering entire API response |
{apiData}
|
{apiData.title}
or JSON.stringify(apiData) |
| Forgetting to map array | {users} where users is [{},{}] | {users.map(u => |
| Directly rendering Date | {new Date()} | {new Date().toISOString()} |
Conclusion
Remember:
-
Never drop a raw object in JSX.
-
Extract the primitives you need or serialise the data for display.
-
Use mapping or formatting helpers to keep components clean, readable, and error‑free.