Try @eslint-react/kit@beta โ†’
logoESLint React

static-components

Validates that components are static, not recreated every render.

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/static-components

Full Name in @eslint-react/eslint-plugin

@eslint-react/static-components

Features

๐Ÿงช

Presets

x recommended recommended-typescript recommended-type-checked strict strict-typescript strict-type-checked

Rule Details

Validates that components are static, not recreated every render. Components that are recreated dynamically can reset state and trigger excessive re-rendering.

Components defined inside other components are recreated on every render. React sees each as a brand new component type, unmounting the old one and mounting the new one, destroying all state and DOM nodes in the process.

If you find yourself wanting to define components inside other components to access local variables, that's a sign you should be passing props instead. This makes components more reusable and testable.

Examples

Invalid

function Parent() {
  // ๐Ÿ”ด Never define a component inside another component!
  const Child = () => <div />;

  return <Child />; // State resets every render
}
function Parent({ type }) {
  // ๐Ÿ”ด Dynamically creating components during render
  const Component = type === "button"
    ? () => <button>Click</button>
    : () => <div>Text</div>;

  return <Component />;
}

Valid

// โœ… Declare components at the top level
function Parent() {
  return <Child />;
}

function Child() {
  return <div />;
}

When a child component needs data from a parent, pass it by props instead of nesting definitions.

Resources

Further Reading


See Also

On this page