react
build_error
ai_generated
true
React Hook useEffect has a missing dependency: 'variable'. Either include it or remove the dependency array.
ID: react/missing-exhaustive-deps
90%Fix Rate
85%Confidence
1Evidence
2023-04-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| eslint-plugin-react-hooks 4.6.0 | active | — | — | — |
| React 18.2.0 | active | — | — | — |
Root Cause
The exhaustive-deps ESLint rule detects that a variable used inside useEffect is not listed in its dependency array, potentially leading to stale closures or missed updates.
generic中文
exhaustive-deps ESLint规则检测到在useEffect内部使用的变量未在其依赖项数组中列出,可能导致过时闭包或错过更新。
Official Documentation
https://react.dev/reference/react/useEffect#specifying-reactive-dependenciesWorkarounds
-
90% success Add the missing variable to the dependency array. If the variable is a function, wrap it in useCallback first.
Add the missing variable to the dependency array. If the variable is a function, wrap it in useCallback first.
-
85% success If the variable is not meant to be reactive (e.g., a stable reference), use useRef to store it and access via .current.
If the variable is not meant to be reactive (e.g., a stable reference), use useRef to store it and access via .current.
中文步骤
Add the missing variable to the dependency array. If the variable is a function, wrap it in useCallback first.
If the variable is not meant to be reactive (e.g., a stable reference), use useRef to store it and access via .current.
Dead Ends
Common approaches that don't work:
-
Adding // eslint-disable-next-line react-hooks/exhaustive-deps without understanding the dependency
80% fail
Suppressing the warning does not fix the underlying stale closure bug; it only hides the lint error.
-
Removing the dependency array entirely ([] to undefined)
70% fail
Without a dependency array, the effect runs after every render, which can cause performance issues and infinite loops if it updates state.
-
Using useRef to store the variable and reading from ref inside the effect
40% fail
This works but is often unnecessary and can be confusing; it also bypasses the reactive nature of the variable.