Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/content/reference/react-dom/flushSync.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,34 +133,34 @@ export default function PrintApp() {

---

## Troubleshooting {/*troubleshooting*/}
## 문제 해결 {/*troubleshooting*/}

### 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*/}
### 오류가 발생합니다: "flushSync was called from inside a lifecycle method" {/*im-getting-an-error-flushsync-was-called-from-inside-a-lifecycle-method*/}


React cannot `flushSync` in the middle of a render. If you do, it will noop and warn:
React는 렌더링 도중에 `flushSync`를 실행할 수 없습니다. 렌더링 도중에 호출하면 아무 작업도 하지 않고 경고를 표시합니다.

<ConsoleBlock level="error">

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.

</ConsoleBlock>

This includes calling `flushSync` inside:
다음 위치에서 `flushSync`를 호출하는 경우도 여기에 해당합니다.

- rendering a component.
- `useLayoutEffect` or `useEffect` hooks.
- Class component lifecycle methods.
- 컴포넌트를 렌더링하는 도중
- `useLayoutEffect` 또는 `useEffect` Hook
- 클래스 컴포넌트의 생명주기 메서드

For example, calling `flushSync` in an Effect will noop and warn:
예를 들어 Effect 안에서 `flushSync`를 호출하면 아무 작업도 하지 않고 경고를 표시합니다.

```js
import { useEffect } from 'react';
import { flushSync } from 'react-dom';

function MyComponent() {
useEffect(() => {
// 🚩 Wrong: calling flushSync inside an effect
// 🚩 잘못된 방법: Effect 안에서 flushSync 호출
flushSync(() => {
setSomething(newValue);
});
Expand All @@ -170,23 +170,23 @@ function MyComponent() {
}
```

To fix this, you usually want to move the `flushSync` call to an event:
이 문제를 해결하려면 보통 `flushSync` 호출을 이벤트로 옮기면 됩니다.

```js
function handleClick() {
// ✅ Correct: flushSync in event handlers is safe
// ✅ 올바른 방법: 이벤트 핸들러에서는 flushSync를 안전하게 호출할 수 있습니다
flushSync(() => {
setSomething(newValue);
});
}
```


If it's difficult to move to an event, you can defer `flushSync` in a microtask:
이벤트로 옮기기 어렵다면 `flushSync`를 마이크로태스크로 미룰 수 있습니다.

```js {3,7}
useEffect(() => {
// ✅ Correct: defer flushSync to a microtask
// ✅ 올바른 방법: flushSync를 마이크로태스크로 미루기
queueMicrotask(() => {
flushSync(() => {
setSomething(newValue);
Expand All @@ -195,10 +195,10 @@ useEffect(() => {
}, []);
```

This will allow the current render to finish and schedule another synchronous render to flush the updates.
이렇게 하면 현재 렌더링을 마친 뒤, 업데이트를 처리하기 위한 동기 렌더링을 한 번 더 예약합니다.

<Pitfall>

`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.
`flushSync`는 성능을 크게 저하할 수 있는데, 이 패턴은 성능에 더 나쁩니다. 마이크로태스크에서 `flushSync`를 호출하는 방법은 탈출구로만 사용하고, 그 전에 다른 모든 방법을 먼저 시도하세요.

</Pitfall>
Loading