no-children-count
Disallows the use of 'Children.count' from the 'react' package.
Full Name in eslint-plugin-react-x
react-x/no-children-countFull Name in @eslint-react/eslint-plugin
@eslint-react/no-children-countPresets
x
recommended
recommended-typescript
recommended-type-checked
strict
strict-typescript
strict-type-checked
Rule Details
Using Children is uncommon and can lead to fragile code. See common alternatives.
Examples
Counting children to conditionally render
Relying on Children.count makes your component tightly coupled to React's internal children representation. Instead, let the parent decide what to render or use standard JavaScript patterns.
For example, if you need to know how many items to display, you can accept an array as a prop directly and use .length:
// Recommended: accept data as an array prop
interface MyComponentProps {
items: string[];
}
function MyComponent({ items }: MyComponentProps) {
return (
<>
<h1>Total rows: {items.length}</h1>
{items.map((item) => (
<div key={item}>{item}</div>
))}
</>
);
}// Problem: Using Children.count to determine the number of children
import React, { Children } from "react";
interface MyComponentProps {
children: React.ReactNode;
}
function MyComponent({ children }: MyComponentProps) {
return (
<>
<h1>Total rows: {Children.count(children)}</h1>
...
</>
);
}Versions
Resources
Further Reading
See Also
react-x/no-children-for-each
Disallows the use ofChildren.forEachfrom thereactpackage.react-x/no-children-map
Disallows the use ofChildren.mapfrom thereactpackage.react-x/no-children-only
Disallows the use ofChildren.onlyfrom thereactpackage.react-x/no-children-to-array
Disallows the use ofChildren.toArrayfrom thereactpackage.