React warns it cannot flush updates because it is already rendering
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.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.
Fix it fast
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/2Match what you see, then jump to its fix
- 1
The stack trace goes through useEffect or useLayoutEffectflushSync runs inside an effectFix - 2
The call must happen on mount, for example opening a dialog when a screen loadsIt cannot move out of the effectFix - 3
The call sits in the component body, not in any effect or handlerA render function triggers the updateFix
- 1
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 devConfirmed by react.dev
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 devConfirmed by react.dev
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 devConfirmed by react.dev
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:
- 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