Fix Uncaught Error Objects are not valid as a React child
By Flavio Copes
Fix React's Objects are not valid as a React child error by rendering a property or list instead of passing a plain object to JSX.
This error might happen because you’ve got a React component and you’re rendering a prop in the JSX, like this, but you forgot to destructure the prop when initializing the component parameters:
function MyComponent(test) {
return <p>{test}</p>
}
You should do this instead:
function MyComponent({ test }) {
return <p>{test}</p>
}
The component receives a props object and we commonly use object destructuring to get the props “mapped” to variables, like {test} in our example.
The same error appears whenever JSX receives a plain object:
const user = { name: 'Flavio' }
return <p>{user}</p> // error
Render a property instead:
return <p>{user.name}</p>
For an array of objects, use map() and return an element with a stable key. JSON.stringify(value) can be useful while debugging, but it is rarely the right user interface.