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

.children() are empty when a component doesn't wrap its children #696

Closed
hon2a opened this issue Nov 18, 2016 · 8 comments
Closed

.children() are empty when a component doesn't wrap its children #696

hon2a opened this issue Nov 18, 2016 · 8 comments

Comments

@hon2a
Copy link

hon2a commented Nov 18, 2016

I have a Localize component that is used with the app as its single child and provides context to it. The relevant part is:

class Localize extends React.Component {
  render() {
    return React.Children.only(this.props.children);
  }
}

When I mount this component and try to get .children(), I get nothing.

const children = mount(React.createElement(Localize, {}, childElement)).children();
// children.length === 0

I can get the wrapper for the child component if I do this:

const child = mount(React.createElement(Localize, {}, childElement))
  .find(childElement.type)
  .at(0);
@ljharb
Copy link
Member

ljharb commented Dec 8, 2016

When you say someComponent - children need to be Elements, not Components - can you share the code that initializes someComponent?

@hon2a
Copy link
Author

hon2a commented Dec 8, 2016

@ljharb Sorry, I see how that is misleading. I updated the description to clearly describe it as Element. It can be any Element, e.g. <div/> or <MyComponent ... /> where MyComponent extends React.Component. The child element is unimportant as of itself, the issue is with how the outer component renders the child directly in its render() without a wrapper.

@ljharb
Copy link
Member

ljharb commented Dec 8, 2016

Does this same thing happen with shallow, or only with mount?

@hon2a
Copy link
Author

hon2a commented Dec 8, 2016

I've just tested it with shallow:

    class Wrapper extends React.Component {
      render() {
        return React.Children.only(this.props.children);
      }
    }

    const wrapper = shallow(
      React.createElement(Wrapper, {}, React.createElement('div'))
    );

    expect(wrapper.children().length).toBe(1);

and it fails (expects 1, gets 0), just like when I use mount.

@hon2a hon2a changed the title .children() returns empty Array when component doesn't wrap its children .children() are empty when a component doesn't wrap its children Dec 8, 2016
@jantorestolsvik
Copy link

Does anyone have an update on this or any idea how we can fix it?

@ljharb
Copy link
Member

ljharb commented Jun 8, 2017

Related to #977?

@hon2a
Copy link
Author

hon2a commented Jun 9, 2017

@ljharb Yeah, looks #977 is a duplicate of this, though with a tad more extensive description.

@ljharb
Copy link
Member

ljharb commented Jul 6, 2018

I don't think this is still an issue in v3; please file a new issue if that's not the case.

@ljharb ljharb closed this as completed Jul 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants