Skip to content

React.Children.count is incorrect for false value #7685

@bgw

Description

@bgw

Do you want to request a feature or report a bug?

I think this is a bug.

What is the current behavior?

If you try to use React.Children.count, and the children contains a child with the value false, the result will be off by one. For example,

function Root() {
    return (
    <Hello>
      {false}
    </Hello>
  );
}

function Hello({children}) {
  return (
    <div>
      Count: {React.Children.count(children)}
      <br />
      toArray length: {React.Children.toArray(children).length}
    </div>
  );
}

Will render

Count: 1
toArray length: 0

If you replace false with null or [] the count will be 0.

Here's a fiddle that demonstrates the issue.

What is the expected behavior?

The count shouldn't include false nodes.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

I've repro'd this on Firefox 48 and Chrome 53. I don't know if this worked in previous versions of React.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions