- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: tsx-react/no-nested-components
Language: TypeScript
Severity: Warning
Category: Best Practices
In TypeScript, particularly when using React, it’s important to avoid nesting components inside other components. Components are meant to be reusable, independent, and encapsulated.
When components are nested, it could lead to unnecessary re-rendering and performance degradation, as the nested component is re-created every time the parent component renders.
To avoid this, define your components at the top level of your module and then use them inside other components by referencing their names. This way, the components are only created once and reused, leading to better performance and easier code management.
function Foo() {
function Bar() {
return <h1> heading </h1>;
}
return (
<>
<Bar />
</>
);
}
function Test() {
return (
<div>
<InnerTest footer={ () => <div /> } /> { }
</div>
);
}
class View extends React.Component {
render() {
function Nested() {
return <h2> nested heading </h2>;
}
return (
<div>
<Nested />
</div>
);
}
}
function Bar(props) {
return <h1> heading </h1>;
}
function Foo() {
return (
<>
<Bar />
</>
);
}
function Test() {
return <InnerTest footer={ <div /> } />;
}
class View extends React.Component {
render() {
return (
<div>
<Nested />
</div>
);
}
}
|
|
For more information, please read the Code Analysis documentation
Identify code vulnerabilities directly in yourVS Code editor
Identify code vulnerabilities directly inJetBrains products