src/content/docs/linter/rules/no-nested-component-definitions.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JSX and TSX" icon="seti:javascript"> ## Summary - Rule available since: `v2.0.0` - Diagnostic Category: [`lint/correctness/noNestedComponentDefinitions`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule doesn't have a fix. - The default severity of this rule is [**error**](/reference/diagnostics#error). - This rule belongs to the following domains: - [`react`](/linter/domains#react) - Sources: - Same as [`react-x/no-nested-components`](https://eslint-react.xyz/docs/rules/no-nested-components) - Same as [`@eslint-react/no-nested-components`](https://eslint-react.xyz/docs/rules/no-nested-components) - Same as [`@eslint-react/no-nested-component-definitions`](https://eslint-react.xyz/docs/rules/no-nested-component-definitions){
"linter": {
"rules": {
"correctness": {
"noNestedComponentDefinitions": "error"
}
}
}
}
Disallows defining React components inside other components.
Component definitions inside other components cause them to be recreated on every render, which can lead to performance issues and unexpected behavior.
When a component is defined inside another component:
A new component is created every time ParentComponent renders:
function ParentComponent() {
function ChildComponent() {
return <div>Hello</div>;
}
return <ChildComponent />;
}
Even with memo, a new component is still created on each render:
function ParentComponent() {
const MemoizedChild = memo(() => {
return <div>Hello</div>;
});
return <MemoizedChild />;
}
Component is defined outside other components:
function ChildComponent() {
return <div>Hello</div>;
}
function ParentComponent() {
return <ChildComponent />;
}
function ChildComponent() {
return <div>Hello</div>;
}
function ParentComponent() {
return <ChildComponent />;
}
function ParentComponent({ CustomComponent }) {
return <CustomComponent />;
}
function ParentComponent({ children }) {
return <div>{children}</div>;
}