- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- Administrator's Guide
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: jsx-react/improper-hook-call
Language: JavaScript
Severity: Warning
Category: Error Prone
This rule ensures proper usage of React hooks, a feature introduced in React version 16.8. Hooks are intended to simplify the state and lifecycle behavior between different components in your application. They should always be called at the top level of your React function to ensure they follow the same order of execution between multiple render phases.
Incorrect usage of hooks can lead to bugs that are difficult to track down. For example, calling hooks conditionally or inside loops, if statements, or nested functions can lead to inconsistent hook calls between renders, which can lead to unexpected behavior and bugs in your application.
To avoid violating this rule, always ensure hooks are used at the top level of your React functions and not inside loops, conditions, or nested functions. This ensures that hooks are called in the same order on every render, which is crucial for their correct operation.
function Name() {
const [country, setCountry] = useState('US');
if (country) {
useEffect(function() {
localStorage.setItem('country', country);
});
} else {
useEffect();
}
return <div>{ displayFlag() }</div>
}
function Name() {
const [country, setCountry] = useState('US');
useEffect(function() {
if (country) {
localStorage.setItem('country', country);
}
});
const [name] = useState('United States');
return <div>{ name }</div>
}
// Custom hooks are fine
function useFoo() {
const [foo, setfoo] = useState('');
return { foo, setfoo };
}
|
|
For more information, please read the Code Analysis documentation
Identify code vulnerabilities directly in yourVS Code editor
Identify code vulnerabilities directly inJetBrains products