React warns when you pass an async function to useEffect
It looks like you wrote useEffect(async () => ...) or returned a Promise.Short answer
An async function always returns a Promise, but React only accepts nothing or a cleanup function from an effect. Keep the effect callback synchronous, declare an async function inside it and call it. Add an ignore flag in the cleanup so a slow old response cannot overwrite newer state.
Fix it fast
1Find out which cause you have
Find the effect and see whether its callback starts with async or returns a value.
grep -rn "useEffect(async" src/2Match what you see, then jump to its fix
- 1
It looks like you wrote useEffect(async () => ...) or returned a Promise.The effect callback itself is asyncFix - 2
The screen shows data for the previous prop or id after you changed it quicklyAn older response arrives after a newer oneFix - 3
Two identical requests in the Network tab, only while running the dev serverStrict Mode runs the effect twice in developmentFix
- 1
What does the error look like?
It looks like you wrote useEffect(async () => ...) or returned a Promise.Two requests in the Network tab on every page load in development
Cause 1 of 3
The effect callback itself is async
1Check
The useEffect reference says the setup function may optionally return a cleanup function. An async callback returns a Promise instead.
grep -rn "useEffect(async" src/2Fix
Move the async work into a function declared inside the effect and call it. The callback itself returns nothing.
useEffect(() => {
async function fetchData() {
const response = await MyAPI.getData(someId);
// use response here
}
fetchData();
}, [someId]);3Verify
Reload; the warning no longer appears in the console.
npm run devConfirmed by react.dev
Cause 2 of 3
An older response arrives after a newer one
1Check
Responses can arrive in a different order than they were sent, so a late answer overwrites the right one.
grep -rn "set[A-Z][A-Za-z]*(" src/components/Profile.jsx2Fix
Use an ignore flag set to true in the cleanup, and only set state when it is still false.
useEffect(() => {
let ignore = false;
async function startFetching() {
const json = await fetchTodos(userId);
if (!ignore) {
setTodos(json);
}
}
startFetching();
return () => {
ignore = true;
};
}, [userId]);3Verify
Switch the id quickly; only the response for the current id is shown.
npm run devConfirmed by react.dev
Cause 3 of 3
Strict Mode runs the effect twice in development
1Check
In development Strict Mode runs one extra setup and cleanup cycle. The docs say you will see two fetches, and production makes one.
grep -rn "StrictMode" src/2Fix
Nothing is broken if you have the ignore flag. If the duplicate bothers you, the React docs suggest a data-fetching library that caches and deduplicates requests.
// the ignore flag from the previous cause already makes the extra
// request harmless; no code change is needed3Verify
Build for production and confirm one request.
npm run build && npm run startConfirmed by react.dev
How do I stop it coming back?
- Declare async functions inside the effect, never as the effect.
- Always add the ignore flag when an effect fetches data.
- Use a data library when many components fetch the same data.
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
The exhaustive-deps warning on useEffect, and how to clear it properly
React Hook useEffect has a missing dependency: 'fetchData'
The linter found a prop, state value or function the effect reads that is not in its dependency array.
4 causes, each with a fixWeb 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 fix