Locales:
en
ko
flushSync
1import { flushSync } from "react-dom"; 2 3// Handle updates synchronously using flushSync 4flushSync(() => { 5 doSomething(); // Do something to update 6}); 7 8// On this line, the Dom has been updated.
The flushSync
function in the react-dom
package is the method used to perform synchronous updates in React. Normally, updates in React are handled asynchronously, with changes in the update queue being processed in the next render cycle. However, the flushSync
function allows you to immediately process updates in callbacks inside that method and proceed with rendering synchronously.
The roles and use cases for flushSync
are as follows
- Synchronous rendering:
flushSync
allows you to bypass React's normal asynchronous update process and process updates immediately to render synchronously. - User Experience: Used to minimize rendering delays that occur when rendering is handled asynchronously in certain situations. For example, this is useful when changes need to be reflected on screen immediately in response to user input.
Caution.
flushSync
should be used with caution.
- Synchronous rendering is risky: Synchronous rendering can impact performance and, if used incorrectly, can make your application less responsive, so it should be used with caution.
- React 18: The
flushSync
function was introduced in React 18 and may not be available in earlier versions, so you should check its availability with your React version. - Do I need it?: It's not common to use
flushSync
. If your app only uses the React API and doesn't integrate with third-party libraries, you probably don't needflushSync
. - Be careful if you use it with
Suspense
: If you useSuspense
,flushSync
can force a fallback state to be displayed.
관련 포스트가 4개 있어요.
useSyncExternalStore가 무엇일까?
짧.
2023/10/23 (updated)
React 18 useSyncExternalStore에 대해서
CSS flex box의 align-items의 flex-start와 baseline의 차이점에 대해서
짧.
2023/09/24
CSS에서 align-items의 flex-start와 baseline의 차이점
React 18.0.0 react-dom의 flushSync의 사용법과 주의사항에 대해서 알아보자.
짧.
2023/09/24
React flushSync에 대해서
maxLength를 넣고 이모지를 넣으면 브라우저마다 계산되는 것이 다르다.
짧.
2023/09/15