logoESLint React
Rules

no-implicit-children

Prevents implicitly passing the 'children' prop to components.

This rule is currently in rc and only available in v3.0.0 rc releases.

This rule is experimental and may change in the future or be removed. It is not recommended for use in production code at this time.

Full Name in eslint-plugin-react-x

react-x/no-implicit-children

Full Name in @eslint-react/eslint-plugin

@eslint-react/no-implicit-children

Features

๐Ÿ’ญ ๐Ÿงช

Presets

Rule Details

This makes it hard to see whether the children was passed correctly to the element or where it came from.

The following cases are allowed and will not be reported:

  1. The children property originates from React's own type definitions (ex: React.DOMAttributes.children, React.PropsWithChildren.children), such as when spreading React.ComponentProps<"div">, React.HTMLAttributes<T>, React.PropsWithChildren<T>, or similar React-provided types.
  2. The children property's type is a React-defined children type alias, such as React.ReactNode, React.ReactElement, React.ReactPortal, or JSX.Element, even if the property is declared in a user-defined type.

Common Violations

Invalid

import React from "react";

declare let someValues: { id: string; className: string; children: string };

function MyComponent() {
  return <div {...someValues} />;
  //          ^^^ This spread attribute implicitly passes the 'children' prop to a component, this could lead to unexpected behavior. If you intend to pass the 'children' prop, use 'children={value}'.
}

Valid

import React from "react";

declare let someValues: { id: string; className: string; children: string };

function MyComponent() {
  const { children, ...rest } = someValues;
  //      ^^^ Dropping the 'children' prop from the spread attributes to prevent implicitly passing it to the component.
  return <div {...rest}>{children}</div>;
}
import React from "react";

declare let someValues: { id: string; className: string; children: React.ReactNode };

function MyComponent() {
  return <div {...someValues} data-slot="my-component" />;
  //          ^^^ The 'children' property is typed as React.ReactNode, which is a React-defined type alias. Allowed.
}
import React from "react";

function PaginationItem({ ...props }: React.ComponentProps<"li">) {
  return <li data-slot="pagination-item" {...props} />;
  //                                     ^^^ The 'children' property originates from React.DOMAttributes.children. Allowed.
}

Resources


See Also

On this page