react · JavaScript
The message
Objects are not valid as a React child (found: object with keys {name, id})
What it means
The value you asked React to render is an object rather than a string or a number, and the list of keys in parentheses tells you which object it is. Usually the line should read {user.name} where it says {user}, or you tried to render a whole response. If you see found: [object Promise] instead of a key list, the cause is different: you rendered the result of an async function without awaiting it. Picking the field to display is the fix; JSON.stringify(user) is fine for a quick look while debugging, but do not leave it on screen.
The fix
There is no one-line command for this. The explanation says what to look at instead.
- Printed by
- react
- JavaScript
- 15
Browser and Node errors usually tell you what broke and not why the value became what it was — you read undefined, it was not a function, it was not JSON — so the place to fix is upstream of the line that threw, and silencing just that line with ?. and default values makes the same problem reappear further away in a form that is harder to recognise.
Reading an error message
- Read from the first line down. The lower you go the more it is about the tool’s internals; the cause is usually at the top.
- If there is a file and a line number, start there — not the top stack frame, but the topmost line that names a file you wrote.
- Search the message verbatim, but strip your own paths and variable names first; those are what stop the search from matching.
- The same condition is worded differently across tool versions. If results look wrong, add the version number to the query.
- Before pasting a fix, check what it throws away. Some of these cannot be undone.
Common questions
Q. What does “Objects are not valid as a React child (found: object with keys {name, id})” mean?
The value you asked React to render is an object rather than a string or a number, and the list of keys in parentheses tells you which object it is. Usually the line should read {user.name} where it says {user}, or you tried to render a whole response. If you see found: [object Promise] instead of a key list, the cause is different: you rendered the result of an async function without awaiting it. Picking the field to display is the fix; JSON.stringify(user) is fine for a quick look while debugging, but do not leave it on screen.
Q. How do I fix it?
There is no one-line command. The explanation above says what to look at instead.
Q. Which tool prints this?
react. It sits under JavaScript, and the message runs to 14 words.