React.Children.onlyChild throws on text child
See original GitHub issueText child is a string so isValidComponent
fails. Would it make sense for it to accept a string? Frankly I’m confused by this API…
Issue Analytics
- State:
- Created 10 years ago
- Comments:11 (6 by maintainers)
Top Results From Across the Web
javascript - "React.Children.only expected to receive a single ...
It seems that <TouchableHighlight> must have exactly one child. The docs say that it supports only one child and more than one must...
Read more >React Top-Level API
React.Children.only(children). Verifies that children has only one child (a React element) and returns it. Otherwise this method throws an error.
Read more >A deep dive into children in React - Max Stoibers Blog
This returns the only child in this.props.children . If there is more than one child, it throws an error, thusly grinding the entire...
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 children only expected receive single child
The error "React.Children.only expected to receive single React element child" occurs when we pass multiple child elements to a component that expects only ......
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
The error message for this is:
Invariant Violation: onlyChild must be passed a children with exactly one child.
This led me incorrectly down the path of thinking I had passed in multiple children until I dug in and realized it was because I had a string instead of aReactElement
. Could this error message a bit more nuanced for cases such as this?I actually already improved this message in 15.3.1: https://github.com/facebook/react/blob/v15.3.1/src/isomorphic/children/onlyChild.js#L34.