Skip to content

Commit 1c4a0dd

Browse files
committed
formatting and pitfall
1 parent 812e7e8 commit 1c4a0dd

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/content/reference/react-dom/flushSync.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,13 @@ Warning: flushSync was called from inside a lifecycle method. React cannot flush
149149

150150
This includes calling `flushSync` inside:
151151

152-
- During the render phase of a component.
152+
- rendering a component.
153153
- `useLayoutEffect` or `useEffect` hooks.
154154
- Class component lifecycle methods.
155155

156-
For example, calling `flushSync` in an Effect will noop and warn:
156+
For example, calling `flushSync` in an Effect will noop and warn:
157157

158-
```js {1-2,4-6}
158+
```js
159159
import { useEffect } from 'react';
160160
import { flushSync } from 'react-dom';
161161

@@ -173,18 +173,19 @@ function MyComponent() {
173173

174174
To fix this, you usually want to move the `flushSync` call to an event:
175175

176-
```js {2-6}
176+
```js
177177
function handleClick() {
178178
// ✅ Correct: flushSync in event handlers is safe
179179
flushSync(() => {
180180
setSomething(newValue);
181181
});
182182
}
183+
```
183184

184185

185-
If it's difficult to move to an event, you can move the flush outside render using a microtask:
186+
If it's difficult to move to an event, you can defer `flushSync` in a microtask:
186187

187-
```js {3-7}
188+
```js {3,7}
188189
useEffect(() => {
189190
// ✅ Correct: defer flushSync to a microtask
190191
queueMicrotask(() => {
@@ -193,5 +194,12 @@ useEffect(() => {
193194
});
194195
});
195196
}, []);
196-
197197
```
198+
199+
This will allow the current render to finish and schedule another syncronous render to flush the updates.
200+
201+
<Pitfall>
202+
203+
`flushSync` can significantly hurt performance, but this particular pattern is even worse for performance. Exhaust all other options before calling `flushSync` in a microtask as an escape hatch.
204+
205+
</Pitfall>

0 commit comments

Comments
 (0)