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.

Children.only can not be called on result of Children.map

See original GitHub issue

Children.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:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Thinkscapecommented, Jul 28, 2017

This function is a mess…

The docs say:

Returns the only child in children. Throws otherwise.

Which suggests that I can pass it the children object (like I do for every other React.Children.* method) and it will give me the first and only child.

The docblock for the function states:

/**
 * Returns the first child in a collection of children and verifies that there
 * is only one child in the collection.
 *

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 😕

1reaction
adamhensoncommented, Jan 9, 2017

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 the children argument, then I don’t see how it makes any sense why React.Children.only should be anything but what @epsitec suggested.

UPDATE: Based on the documentation, I was under the impression that the children argument received by React.createElement needs to be an array data type. Unfortunately, if there is a single child - in order to accommodate code that uses React.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:

if (Array.isArray(children) && children.length === 1) {
    children = children[0];
}

return React.createElement(type, props, children);
Read more comments on GitHub >

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

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