New ChildrenBuilder breaking React.Children.only
See original GitHub issueIt seems like Children.only
does not work anymore with ChildrenBuilder
.
Indeed, instead of an opaque structure, PropsWithChildren
now has an Array
for children
, which cause Children.only
to always fail.
This failure can be understood through the reading of its implementation which depends on isValidElement.
As a workaround I now use:
const val ReactOnlyChildError = "React.Children.only expected to receive a single React element child"
fun onlyChild(children: ReadonlyArray<ReactNode>?): ReactElement {
if ((children?.size ?: 0) > 1) error("$ReactOnlyChildError (more than one were provided).")
return children?.getOrNull(0)?.asElementOrNull() ?: error("$ReactOnlyChildError (none were provided).")
}
However, maybe Children.only
should be removed and replaced by a working usage (such as the one above).
I also wonder if there are other cases where the new ChildrenBuilder
breaks functions provided by React
.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:12 (4 by maintainers)
Top Results From Across the Web
Uncaught Error: React.Children.only expected to receive a ...
js:1251 Uncaught Error: React.Children.only expected to receive a single React element child".I am totally stuck and not able to find a solution ...
Read more >React Top-Level API
Verifies that children has only one child (a React element) and returns it. Otherwise this method throws an error. Note: React.Children.only() does not...
Read more >React Children And Iteration Methods - Smashing Magazine
Let's break that down: Returns the children opaque data structure as a flat array. With keys assigned to each child. The first point...
Read more >React.Children.only expected to receive a single ... - GitHub
Children.only expected to receive a single React element child The ... That's why using a React component as a direct child of <Step>...
Read more >A deep dive into children in React - Max Stoibers Blog
We can manipulate children in React using the power of JavaScript. Let's explore children in-depth and see how they can make our lives ......
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
Since
pre.283
children
isReactNode
It depends on your component - no silver bullet.