The exhaustive-deps warning on useEffect, and how to clear it properly
React Hook useEffect has a missing dependency: 'roomId'. Either include it or remove the dependency arrayShort answer
The linter found a prop, state value or function the effect reads that is not in its dependency array. Do not delete the check. Change the code: move the value or function inside the effect, move constants outside the component, pass primitives, or read live values through an Effect Event.
Fix it fast
1Find out which cause you have
Read which name the warning mentions, then find where it is declared. Props, state and anything defined in the component body count as reactive.
npx eslint src --rule '{"react-hooks/exhaustive-deps": "warn"}'2Match what you see, then jump to its fix
- 1
React Hook useEffect has a missing dependency: 'fetchData'A function defined in the component is called inside the effectFix - 2
The warning names an object such as 'options', or the effect re-runs on every renderAn object or array created during render is read by the effectFix - 3
Adding the dependency would reconnect, refetch or restart something on every changeThe effect should read the latest value but not re-run when it changesFix - 4
An effect watches a flag like 'submitted' and then sends a requestThe code belongs in an event handler, not an effectFix
- 1
What does the error look like?
React Hook useEffect has a missing dependency: 'roomId'. Either include it or remove the dependency arrayReact Hook useEffect has a missing dependency: 'fetchData'react-hooks/exhaustive-deps
Cause 1 of 4
A function defined in the component is called inside the effect
1Check
A function declared in the component body is a new value on every render, so the linter wants it in the array. Putting it there would re-run the effect on every render.
grep -n "useEffect" -A8 src/components/*.jsx2Fix
If only the effect uses the function, declare it inside the effect. React's guide shows this as the first way to remove a dependency.
useEffect(() => {
function load() {
return fetch(`/api/items?room=${roomId}`).then(r => r.json());
}
load().then(setItems);
}, [roomId]);3Verify
Run the linter again; the warning for that effect should be gone.
npx eslint srcConfirmed by react.dev
Cause 2 of 4
An object or array created during render is read by the effect
1Check
Objects and functions are new on every render, so they re-trigger effects even when their contents are the same.
grep -n "const options" -B2 -A6 src/components/*.jsx2Fix
If the object never depends on props or state, move it above the component. If it does, read its primitive fields (strings, numbers) and depend on those.
const options = { serverUrl: "https://localhost:1234", roomId: "music" };
function ChatRoom() {
useEffect(() => {
const connection = createConnection(options);
connection.connect();
return () => connection.disconnect();
}, []);
}3Verify
The linter stops warning, and the effect runs once on mount in the browser.
npx eslint srcConfirmed by react.dev
Cause 3 of 4
The effect should read the latest value but not re-run when it changes
1Check
Typical cases are an onMessage callback prop or a flag such as isMuted that is only read when an event arrives.
grep -rn "useEffect" src/ | head2Fix
Wrap the reading code in an Effect Event with useEffectEvent, call it from the effect, and leave it out of the array. Effect Events are not reactive.
const onMessage = useEffectEvent(msg => {
setMessages(m => [...m, msg]);
if (!isMuted) playSound();
});
useEffect(() => {
const connection = createConnection();
connection.connect();
connection.on("message", msg => onMessage(msg));
return () => connection.disconnect();
}, [roomId]);3Verify
Toggle isMuted while connected. The connection stays open and the new value is used.
npx eslint srcConfirmed by react.dev
Cause 4 of 4
The code belongs in an event handler, not an effect
1Check
If the work happens because the user clicked something, it does not need an effect at all.
grep -rn "useEffect" -A6 src/ | grep -E "submitted|clicked"2Fix
Move the request into the handler itself, and delete the effect and the flag.
function handleSubmit(e) {
e.preventDefault();
post("/api/register", { firstName, lastName });
}3Verify
Submit the form once and check the network tab shows exactly one request.
npm startConfirmed by react.dev
How do I stop it coming back?
- Treat the dependency warning like a compile error, as React's own guide advises.
- Never silence it with eslint-disable-next-line react-hooks/exhaustive-deps; the guide shows it freezes an old value inside the effect.
- Keep helper functions inside the effect or above the component when they do not use props or state.
- Fetch with a cleanup flag so a late response from an older request is ignored.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fix