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

Why function as child is not considered as children? #15128

Closed
ri8ika opened this issue Mar 16, 2019 · 3 comments
Closed

Why function as child is not considered as children? #15128

ri8ika opened this issue Mar 16, 2019 · 3 comments

Comments

@ri8ika
Copy link

ri8ika commented Mar 16, 2019

After digging deeper with this question, I have found function as a child is not being considered as children.

Take this example:

class CountExpression extends React.Component {
  render() {
    const children = React.Children.toArray(this.props.children)
    console.log(children)
    return <p>{React.Children.count(children)}</p>
  }
}
<CountExpression>
   {'one'}
   {'two'}
   { () => <p>Still, this will be ignored as child. Why?</p>}
   <p>This will be included in array - that's fine</p>
</CountExpression>

So, I would like know why is it so? Is it a bug or an expected behavior?

@TryingToImprove
Copy link
Contributor

() => <p>Still, this will be ignored as child. Why?</p> creates a new component just like;

const MyComponent = () => <p>Still, this will be ignored as child. Why?</p>

@ri8ika
Copy link
Author

ri8ika commented Mar 19, 2019

I think it's a bug rather! I have updated my answer there in SO where you can see this.props.children.length shows the result by counting even the function as child component whilst React.Children.count(this.props.children) doesn't count function as child.

@behzad888
Copy link
Contributor

dup #11888

@necolas necolas closed this as completed Apr 11, 2019
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

5 participants