Maaz Zindani

Loading the index…

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

React warns it cannot flush updates because it is already rendering

ErrorWarning: flushSync was called from inside a lifecycle method. React cannot flush when React is already rendering. Consider moving this call to a scheduler task or micro task.

Short answer

flushSync forces a synchronous render, which React cannot do while it is already rendering. Something calls it during render, in a hook or in a lifecycle method. Move that call into an event handler. If that is hard, wrap it in queueMicrotask so the current render finishes first.

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

Fix it fast

  1. 1Find out which cause you have

    Open the browser console stack trace of the warning and find the first frame from your own code. That is the call that runs during render.

    // look for flushSync in the trace, or in your dependencies:
    grep -rn "flushSync" src/
  2. 2Match what you see, then jump to its fix

    1. 1The stack trace goes through useEffect or useLayoutEffectflushSync runs inside an effectFix
    2. 2The call must happen on mount, for example opening a dialog when a screen loadsIt cannot move out of the effectFix
    3. 3The call sits in the component body, not in any effect or handlerA render function triggers the updateFix

What does the error look like?

  • Warning: flushSync was called from inside a lifecycle method. React cannot flush when React is already rendering. Consider moving this call to a scheduler task or micro task.

Cause 1 of 3

flushSync runs inside an effect

1Check

React does not flush synchronously while rendering, so flushSync in a render, an effect or a class lifecycle method does nothing and logs this warning.

grep -rn "flushSync" -B 3 src/ | grep -E "useEffect|useLayoutEffect"

2Fix

Call flushSync from an event handler. Event handlers are the safe place.

function handleClick() {
  flushSync(() => {
    setSomething(newValue);
  });
}

3Verify

Trigger the action; the warning no longer appears.

npm run dev

Confirmed by react.dev

Did this fix it?

Cause 2 of 3

It cannot move out of the effect

1Check

Some calls depend on mount timing. The React page gives a fallback for this case, with a cost to performance.

grep -rn "useEffect" -A 6 src/ | grep -iE "alert|modal|swal"

2Fix

Defer the flushSync call to a microtask, so the current render completes first.

useEffect(() => {
  queueMicrotask(() => {
    flushSync(() => {
      setSomething(newValue);
    });
  });
}, []);

3Verify

Reload; the dialog appears and the warning is gone.

npm run dev

Confirmed by react.dev

Did this fix it?

Cause 3 of 3

A render function triggers the update

1Check

Code written directly in a component body runs during render. A helper that opens an alert or updates another root from there will hit this warning.

grep -rn "flushSync\|Swal\.\|alert(" src/components/

2Fix

Move that call out of the render body. Effects run after the component commits, not during render, but flushSync inside an effect still warns, so use an event handler, or the microtask form if it must happen on mount.

// before: runs on every render
function Page() { openDialog(); return <Main />; }

// after: runs once on mount, deferred
function Page() {
  useEffect(() => {
    queueMicrotask(() => openDialog());
  }, []);
  return <Main />;
}

3Verify

The dialog opens once and no warning is logged.

npm run dev

Confirmed by react.dev

Did this fix it?

How do I stop it coming back?

  • Treat flushSync as a last resort; it can hurt performance.
  • Trigger imperative UI such as alerts and modals from event handlers.
  • When a library calls flushSync for you, call that library from handlers too.

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.