You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/reference/react-dom/flushSync.md
+54Lines changed: 54 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -131,3 +131,57 @@ Without `flushSync`, the print dialog will display `isPrinting` as "no". This is
131
131
Most of the time, `flushSync` can be avoided, so use `flushSync` as a last resort.
132
132
133
133
</Pitfall>
134
+
135
+
---
136
+
137
+
## Troubleshooting {/*troubleshooting*/}
138
+
139
+
### I'm getting an error: "flushSync was called from inside a lifecycle method" {/*im-getting-an-error-flushsync-was-called-from-inside-a-lifecycle-method*/}
140
+
141
+
You might get an error that says: `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.`
142
+
143
+
This happens when you call `flushSync` while React is already rendering, such as inside:
144
+
145
+
- Class component lifecycle methods (`componentDidMount`, `componentDidUpdate`, etc.)
146
+
-`useLayoutEffect` or `useEffect` hooks
147
+
- During the render phase of a component
148
+
149
+
```js {1-2,4-6}
150
+
import { useEffect } from'react';
151
+
import { flushSync } from'react-dom';
152
+
153
+
functionMyComponent() {
154
+
useEffect(() => {
155
+
// 🚩 Wrong: calling flushSync inside an effect
156
+
flushSync(() => {
157
+
setSomething(newValue);
158
+
});
159
+
}, []);
160
+
161
+
return<div>{/* ... */}</div>;
162
+
}
163
+
```
164
+
165
+
To fix this, move the `flushSync` call outside of the rendering cycle:
0 commit comments