no-children-map
Disallows the use of 'Children.map' from the 'react' package.
Full Name in eslint-plugin-react-x
react-x/no-children-mapFull Name in @eslint-react/eslint-plugin
@eslint-react/no-children-mapPresets
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
Wrapping each child in a layout element
Using Children.map to wrap children assumes a specific React internals structure that may break. Instead, accept an array of data or let the parent map over items directly.
For example, if you need to wrap items in a styled container, accept an array of data as a prop and map over it:
// Recommended: accept data as an array prop and map over it
interface MyComponentProps {
items: string[];
}
function MyComponent({ items }: MyComponentProps) {
return (
<div className="RowList">
{items.map((item) => (
<div className="Row" key={item}>
{item}
</div>
))}
</div>
);
}// Problem: Using Children.map to wrap each child in a styled container
import React, { Children } from "react";
interface MyComponentProps {
children: React.ReactNode;
}
function MyComponent({ children }: MyComponentProps) {
return (
<div className="RowList">
{Children.map(children, (child) => <div className="Row">{child}</div>)}
</div>
);
}Versions
Resources
Further Reading
See Also
react-x/no-children-count
Disallows the use ofChildren.countfrom thereactpackage.react-x/no-children-for-each
Disallows the use ofChildren.forEachfrom thereactpackage.react-x/no-children-only
Disallows the use ofChildren.onlyfrom thereactpackage.react-x/no-children-to-array
Disallows the use ofChildren.toArrayfrom thereactpackage.