Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React.Children.onlyChild throws on text child #1104

Closed
chenglou opened this issue Feb 16, 2014 · 11 comments
Closed

React.Children.onlyChild throws on text child #1104

chenglou opened this issue Feb 16, 2014 · 11 comments

Comments

@chenglou
Copy link
Contributor

Text child is a string so isValidComponent fails. Would it make sense for it to accept a string? Frankly I'm confused by this API...

@plievone
Copy link
Contributor

I don't understand the use case of this like this either, making it public at this point may be a mistake?

@slorber
Copy link
Contributor

slorber commented Jul 30, 2014

onlyChild is mentionned in #751 but is it documented somwhere?

@chenglou
Copy link
Contributor Author

@zpao
Copy link
Member

zpao commented Dec 10, 2014

@spicyj @sebmarkbage is this still the case now that we wrapped this?

@sophiebits
Copy link
Collaborator

Nothing here has changed. If you call onlyChild(this.props.children) on a (string) text node, it still isn't a valid ReactElement.

@sebmarkbage
Copy link
Collaborator

It's not an element. It's a ReactNode. Might be possible to have an isValidNode but I thought we have a propType for that already.

@slorber
Copy link
Contributor

slorber commented Dec 17, 2014

Hey just wanted to mention that I encountered this problem on a wrapper component.

It looks like:

var WithTooltip = React.createClass({
    propTypes: {
        children: React.PropTypes.node.isRequired
    },
    render: function() {
        return React.Children.only(this.props.children);
    }
});
module.exports = WithTooltip;

I can't call <WithTooltip>Some text</WithTooltip> as the method throws an error.

I can't either remove the check since it raises Error: Invariant Violation: WithTooltip.render(): A valid ReactComponent must be returned. You may have returned undefined, an array or some other invalid object.

So the solution I found was

    render: function() {
        if (typeof this.props.children === 'string') {
            return <span>{this.props.children}</span>;
        } else {
            return React.Children.only(this.props.children);
        }
    }

Not sure it helps, just wanted to explain my usecase so that you can think of it when taking any action

@brigand
Copy link
Contributor

brigand commented Dec 17, 2014

My understanding was that whatever's in props.children is a child or array of children. Just like <div><span />foo</div> has two children, not one child and one invalid child. If that's agreed upon, how would removing the span result in "foo" no longer being a valid child? We can use React.Children.map in either case without complaint about encountering a non-child.

If nothing else, the name and docs are a bit vague considering how specific the implementation is, and the definition of 'child' is ambiguous. I've never used it, but there's nothing indicating that a string or number isn't a valid value, so I'd assume they were and eventually end up at this issue in a google search while debugging the problem.

@eipark
Copy link

eipark commented Sep 13, 2016

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 a ReactElement. Could this error message a bit more nuanced for cases such as this?

@sophiebits
Copy link
Collaborator

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.

@eipark
Copy link

eipark commented Sep 13, 2016

Cool - that's a bit more specific. As a next step, it would be nice to actually detect and message what exactly is invalid for the most common cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants