no-children-only
Disallows the use of 'Children.only' from the 'react' package.
Full Name in eslint-plugin-react-x
react-x/no-children-onlyFull Name in @eslint-react/eslint-plugin
@eslint-react/no-children-onlyPresets
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
Enforcing a single child
Using Children.only to assert that a component receives exactly one child is fragile. If you need to restrict children, consider documenting the prop type or using a different API design.
For example, if your component needs exactly one child element, you can type the prop as a single ReactElement instead of React.ReactNode:
// Recommended: type the children prop to enforce a single child at compile time
import { type ReactElement } from "react";
interface MyComponentProps {
children: ReactElement;
}
function MyComponent({ children }: MyComponentProps) {
return <div>{children}</div>;
}// Problem: Using Children.only to enforce a single child
import React, { Children } from "react";
interface MyComponentProps {
children: React.ReactNode;
}
function MyComponent({ children }: MyComponentProps) {
const element = Children.only(children);
// ...
}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-map
Disallows the use ofChildren.mapfrom thereactpackage.react-x/no-children-to-array
Disallows the use ofChildren.toArrayfrom thereactpackage.