Children.only can not be called on result of Children.map
See original GitHub issueChildren.only
is intended to be called on an opaque Children
object (eg. this.props.children
). Best as I can tell, Children.map
is intended to return an opaque Children
object. But Children.only
can’t be called on the return value of Children.map
, as per https://github.com/facebook/react/issues/4410#issuecomment-122199087
It seems very reasonable to want to map your only child to another element (clone element, for instance), and then grab the only child from the map and use it to render. It is also very surprising for the identity operation to be passed into map, but the thing that comes out to not be semantically equivalent.
Issue demonstrated here: https://jsfiddle.net/9Ldyq5jk/4/
Issue Analytics
- State:
- Created 8 years ago
- Reactions:2
- Comments:13 (4 by maintainers)
Top Results From Across the Web
Next.js: Error: React.Children.only expected to receive a ...
-> So there are two child nodes (One is space and another is <a> tag) which is invalid and hence such error occurs....
Read more >React Children And Iteration Methods - Smashing Magazine
In this article, we'll discuss and learn about the use case of iterating over React `children` and the ways to do it. In...
Read more >React Top-Level API
React.Children.only Verifies that children has only one child (a React element) and returns it. Otherwise this method throws an error. Note: React.Children. ...
Read more >React Children: The misunderstood prop - Netlify
Let's go through step by step why children are weird, so you can understand them better. Again: React children. Not humans.
Read more >Nephrotic Syndrome in Children | NIDDK
Overview of nephrotic syndrome in children, a combination of symptoms that can develop when a child's kidneys are not working properly.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This function is a mess…
The docs say:
Which suggests that I can pass it the
children
object (like I do for every otherReact.Children.*
method) and it will give me the first and only child.The docblock for the function states:
Which further confirms that’s the expected behaviour…
But in reality it does none of those things. Instead, it just verifies that the arguments is a ReactElement 😕
I’m seeing an outcome of this issue in react-router-bootstrap, when using
React.createElement
directly.https://github.com/react-bootstrap/react-router-bootstrap/issues/195
If
React.createElement
expects an array data type as thechildren
argument, then I don’t see how it makes any sense whyReact.Children.only
should be anything but what @epsitec suggested.UPDATE: Based on the documentation, I was under the impression that the
children
argument received byReact.createElement
needs to be an array data type. Unfortunately, if there is a single child - in order to accommodate code that usesReact.Children.only
- this needs to be a React element object.Yikes. So, I suppose I need to hack my code that uses
React.createElement
to accommodate this.I’ll need to do something like the following: