question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New ChildrenBuilder breaking React.Children.only

See original GitHub issue

It 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
turanskycommented, Dec 21, 2021

Since pre.283 children is ReactNode

1reaction
turanskycommented, Dec 21, 2021

It depends on your component - no silver bullet.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found