React Hook useEffect has a missing dependency
React JavaScript Library
Severity: MinorWhat it means
ESLint, from React's eslint-plugin-react-hooks: React Hook useEffect has a missing dependency: 'userId'. Either include it or remove the dependency array.
With several: has missing dependencies: 'a' and 'b'. Either include them or remove the dependency array. useCallback and useMemo get the same warning.
React's docs: the linter checks that every reactive value the effect reads, such as props, state and anything declared in the component, is in its list.
Affected Models
- React 16.8 and later
How to Fix It
-
Add what it names to the array: [userId]. That's the usual fix.
-
Adding it makes the effect run too often? Functions and objects made in the component are new on every render. React's docs: move them inside the effect, or outside the component.
-
The effect reads state only to set its next value? Use an updater function, setCount(c => c + 1), and the dependency goes away.
-
React's docs warn against silencing the rule with an eslint-disable comment. When the dependencies don't match the code, the risk of bugs is very high.