Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 1.4 — What JSX Can Render
React · Example 3 of 3

An object as a child crashes the render

The page stays blank and React explains why: it found an object with keys name and city. Fix it by writing {user.name} (or {user.name} from {user.city}).

Inside JSX, strings and numbers become text, elements become DOM, arrays render each item, and null, undefined, true and false render nothing; but 0 renders as "0", and plain objects crash with "Objects are not valid as a React child".

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write a Cart component with a count prop that shows Cart plus a badge with the number only when count is above 0. Test it with 0 and 5.

02

Given const order = { id: 7, total: 450 }, render Order #7: ₹450 without crashing.

Explain it without notes

01

Which values render as nothing in JSX, and why is that useful?

02

Explain the 0 && bug and three ways to fix it.

03

What does "Objects are not valid as a React child" mean, and how do you fix it?

04

What's the difference between {Hello} and <Hello />?

An object as a child crashes the render

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

Uncaught Error: Objects are not valid as a React child (found: object with keys {name, city}). If you meant to render a collection of children, use an array instead.
An error occurred in the <p> component.

Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://react.dev/link/error-boundaries to learn more about error boundaries.