Try @eslint-react/kit@beta
logoESLint React

no-children-only

Disallows the use of 'Children.only' from the 'react' package.

Full Name in eslint-plugin-react-x

react-x/no-children-only

Full Name in @eslint-react/eslint-plugin

@eslint-react/no-children-only

Presets

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

On this page