DEV Community

Cristian Sifuentes
Cristian Sifuentes

Posted on

Mastery: Fixing "Objects are not valid as a React child" Error

Objects are not valid as a React child<br>

Mastery: Fixing "Objects are not valid as a React child" Error

In modern React development, seemingly small mistakes can lead to cryptic and frustrating errors. One such classic example is:

Uncaught Error: Objects are not valid as a React child (found: object with keys {email, name}).
Enter fullscreen mode Exit fullscreen mode

In this guide, we'll break down this error, understand what causes it, and build a complete step-by-step form using useState — the right way.


The Problem Code

import React, { useState } from 'react';

export const Forms = () => {
  const [formData, setFormData] = useState({
    email: '',
    name: '',
  });

  return (
    <form autoComplete='off' className='w-50'>
      <div className="mb-3">
        <label className="form-label">Email:</label>
        <input type="email" className='form-control' name='email' />
      </div>
      <div className="mb-3">
        <label className="form-label">Name:</label>
        <input type="text" className='form-control' name='name' />
      </div>

      <pre>{ formData }</pre> {/* 🚨 This is the error */}
    </form>
  );
};
Enter fullscreen mode Exit fullscreen mode

Why This Fails

You're trying to render an object directly with { formData }, but React can only render strings, numbers, arrays, or elements — not plain objects.

This line:

<pre>{ formData }</pre>
Enter fullscreen mode Exit fullscreen mode

throws:

Uncaught Error: Objects are not valid as a React child
Enter fullscreen mode Exit fullscreen mode

The Correct Solution

Use JSON.stringify() to serialize the object:

<pre>{ JSON.stringify(formData, null, 2) }</pre>
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Working Example

import React, { useState } from 'react';

export const Forms = () => {
  const [formData, setFormData] = useState({
    email: '',
    name: '',
  });

  const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    const { name, value } = e.target;
    setFormData(prev => ({
      ...prev,
      [name]: value
    }));
  };

  return (
    <form autoComplete='off' className='w-50'>
      <div className="mb-3">
        <label className="form-label">Email:</label>
        <input
          type="email"
          name="email"
          className='form-control'
          value={formData.email}
          onChange={handleChange}
        />
      </div>
      <div className="mb-3">
        <label className="form-label">Name:</label>
        <input
          type="text"
          name="name"
          className='form-control'
          value={formData.name}
          onChange={handleChange}
        />
      </div>

      <h5 className='mt-3'>Form JSON Output:</h5>
      <pre>{ JSON.stringify(formData, null, 2) }</pre>
    </form>
  );
};
Enter fullscreen mode Exit fullscreen mode

Key Concepts

Concept Purpose
useState() Hook for tracking component state
handleChange() General handler for all inputs
...prev Spread operator to retain previous state
JSON.stringify() Converts JS object to renderable string

Summary

React doesn’t allow rendering of plain JavaScript objects directly inside JSX. You must convert objects to strings with JSON.stringify() or display individual properties.

Mastering these basics of state handling and JSX rendering will make you a confident React developer.

⚠️ Always remember: Render strings, not raw objects.


#react #javascript #hooks #formhandling #json #frontend #architecture

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one server—search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more