Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

React warns when you pass an async function to useEffect

ErrorIt 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.

  • Applies to React 18, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/
  2. 2Match what you see, then jump to its fix

    1. 1It looks like you wrote useEffect(async () => ...) or returned a Promise.The effect callback itself is asyncFix
    2. 2The screen shows data for the previous prop or id after you changed it quicklyAn older response arrives after a newer oneFix
    3. 3Two identical requests in the Network tab, only while running the dev serverStrict Mode runs the effect twice in developmentFix

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 dev

Confirmed by react.dev

Did this fix it?

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.jsx

2Fix

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 dev

Confirmed by react.dev

Did this fix it?

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 needed

3Verify

Build for production and confirm one request.

npm run build && npm run start

Confirmed by react.dev

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.