score:8
This error occurs when a javascript object has been included inside some jsx tags.
It is that simple.
Consider the following example:
// A simple javascript object (also a dictionary) containing three keys
const someObject = {
name: 'Simple Javascript object',
id: 123,
someFunction: function() {
return <p>This function returned more jsx!</p>;
}
}
const SimpleFunctionComponent = () => <h2>{someObject}</h2>;
It is important to note that
<h2>{someObject}</h2>
here is not html, it is jsx. React will parse it and create the corresponding elements in the dom using javascript. The contents between/inside two corresponding component tags (like<h2>
and</h2>
here) are considered the child or children of the component.
In this example, if I try and render <SimpleFunctionComponent />
I will get the error
Objects are not valid as a React child (found: object with keys {name, id, someFunction}). If you meant to render a collection of children, use an array instead.
someObject
is not a valid React child. To my knowledge only string
, int
and more jsx are. someObject
is not any of these things.
If I modify SimpleFunctionComponent
slightly it will work.
const SimpleFunctionComponent = () => (
<div>
<h2>STRING: {someObject.name}</h2>
<p>INT: {someObject.id}</p>
<p>JSX: {someObject.someFunction()}</p>
</div>
);
The only real bit of information here to help debug this sort of problem is the error message will provide you with the keys inside of the object causing the error.
In the OP's code the error is coming from this
<Post
key={id}
username={post.username}
caption={post.caption}
imageURL={post.imageURL}
/>;
Note: The code above is completely valid but the error will come from the rendering of the Post
component where any of these attributes are used as a React child.
e.g.
// If Post was a functional component const Post = (props) => { return ( <div> <h1>{props.username}</h1> <p>{props.caption}</p> <img src={props.imageURL} alt="post image"/> </div> ) }
Without knowing what the structure of post.id
or post.post
are all we can deduce is that any one of id
, post.username
, post.caption
, and post.imageURL
is actually an object with the key username inside.
(found: object with keys {username})
.
Lets assume it is post.username
, the OP thought the structure of post
was
const post = {
id: 'some id',
post: {
username: 'some username',
caption: 'some caption',
imageURL: 'some image url'
}
}
when it was actually
const post = {
id: 'some id',
post: {
username: {
username: 'some username'
},
caption: 'some caption',
imageURL: 'some image url'
}
}
and would need to put username={post.username.username}
instead.
This would be the same respectively for id
, post.caption
, and post.imageURL
if they were actually the object causing the error.
Here is a CodeSandox of the example I provided and one replicating the situation the OP had.
Source: stackoverflow.com
Related Query
- React native - objects are not valid as a React child (found: object with keys {$$typeof, type, key, ref, props, _owner, _store})
- Objects are not valid as a React child (found: object with keys {})
- Objects are not valid as a React child (found: object with keys {children}): ReactJS
- React.js Error: " Objects are not valid as a React child (found: object with keys {})."
- Objects are not valid as a React child (found: object with keys {username}). If you meant to render a collection of children, use an array instead
- Error: Objects are not valid as a React child (found: object with keys {id, name})
- Error: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead
- Error: Objects are not valid as a React child (found: object with keys {$$typeof, type, compare, WrappedComponent, displayName})
- Objects are not valid as a React child (found: object with keys {name}). If you meant to render a collection of children, use an array instead
- Error: Objects are not valid as a React child (found: object with keys {}). use an array instead
- Error: Objects are not valid as a React child (found: object with keys {})
- React with Router v5 Error: Objects are not valid as a React child (found: object with keys {children})
- Objects are not valid as a React child (found: object with keys {job}). If you meant to render a collection of children, use an array instead
- Objects are not valid as a React child (found: object with keys {children})
- 'Error: Objects are not valid as a React child (found: object with keys {results, info})'
- Error: Objects are not valid as a React child (found: object with keys {zip})
- Error: Objects are not valid as a React child (found: object with keys {low, high})
- Uncaught Invariant Violation: Objects are not valid as a React child (found: object with keys {$$typeof, render})
- Failed to render a d3js object in Reactjs: Objects are not valid as a React child (found: object with keys {_groups, _parents})
- Objects are not valid as a React child (found: object with keys {totalItems}). If you meant to render a collection of children, use an array instead
- Objects are not valid as a React child (found: object with keys {arr}). If you meant to render a collection of children, use an array instead
- Firebase Error: Objects are not valid as a React child (found: object with keys {seconds, nanoseconds})
- Error: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead. JS
- Objects are not valid as a React child (found: object with keys {weight}). If you meant to render a collection of children, use an array instead
- Objects are not valid as a React child (found: object with keys {..}). .....use an array instead. in Select (created by Context.Consumer)
- I AM HAVING ERROR WHEN TRYING TO MAP A DATA IN REACT . Objects are not valid as a React child (found: object with keys {children}), How do i solve it?
- React , Error: Objects are not valid as a React child (found: object with keys {data})
- What is causing this error - Objects are not valid as a React child (found: object with keys {id, task})
- Error: Objects are not valid as a React child (found: object with keys {name}). If you meant to render a collection of children, use an array instead
- Objects are not valid as a React child (found: object with keys {children}). If you meant to render a collection of children, use an array instead
More Query from same tag
- Passing an array of Object using Provider in React
- Accessing a url parameter to use in a query using react-router-relay
- React not closing dialog box
- Nested React Router path to n level
- Uncaught Error: AllPostsComments.render(): A valid ReactComponent must be returned
- react bootstrap modal - how do I get it to show?
- Can't increase quantity in nested array
- ReactQuill - How to style placeholder attribute?
- How to pass the search query to table filter between different JS files?
- Typescript + React Testing Library - 'SidebarItem' refers to a value, but is being used as a type here. Did you mean 'typeof SidebarItem'?ts(2749)
- Changing attribute in state React.js
- React store promise array of objects in state
- find total sum in react
- React Testing Error: Uncaught [TypeError: Cannot read property 'map' of undefined]
- SetState Is Not A Function on OnChange
- Can't npm install local dependency
- React adding "build" to end of base url
- Error routing user to another component after login
- `display: none` vs conditional render in React
- Mongodb doesn't update
- Show slots based on data in Full Calendar day view
- How do I override compiled classes?
- how to change object value in react hooks?
- React error: Too many re-renders, when i try to conditonally render using a button
- How do I test async fetch in react componentDidMount with chained promises?
- Dynamically add and remove form rows with react
- NodeJs: Serve image, stored on AWS S3, to client img tag
- Upgrade Materialize 1.0.0 -> 1.1.0
- How to check if an array that has objects in react components state has duplicate values before sending a form?
- How to handle saving of a ManyToMany relationship at the same time?