Skip to content

Commit b3222f1

Browse files
KKonziteenie-jo
andauthored
Translate: referencing values with refs (#732)
* translate: 어색한 문장 및 주석 번역 수정 * fix: 잘못 걸려있는 링크 수정 --------- Co-authored-by: teenie.jo <[email protected]>
1 parent ed34616 commit b3222f1

File tree

1 file changed

+127
-127
lines changed

1 file changed

+127
-127
lines changed

src/content/learn/referencing-values-with-refs.md

Lines changed: 127 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: '값을 Refs와 함께 참조하기'
2+
title: 'Ref로 값 참조하기'
33
---
44

55
<Intro>
@@ -35,7 +35,7 @@ const ref = useRef(0);
3535

3636
```js
3737
{
38-
current: 0 // The value you passed to useRef
38+
current: 0 // useRef에 전달한 값
3939
}
4040
```
4141

@@ -70,9 +70,9 @@ export default function Counter() {
7070

7171
ref는 숫자를 가리키지만, [state](/learn/state-a-components-memory)처럼 문자열, 객체, 심지어 함수 등 모든 것을 가리킬 수 있습니다. state와 달리 ref는 읽고 수정할 수 있는 `current` 프로퍼티를 가진 일반 자바스크립트 객체입니다.
7272

73-
**컴포넌트는 모든 증가에 따라 다시 렌더링 되지 않습니다.** state와 마찬가지로 ref도 React에 리렌더에 의해 유지됩니다. 그러나 state를 설정하면 컴포넌트가 다시 렌더링 됩니다. ref를 변경하면 변경되지 않습니다!
73+
**컴포넌트는 모든 증가에 대하여 다시 렌더링 되지 않습니다.** state와 마찬가지로 ref도 React에 리렌더에 의해 유지됩니다. 그러나, state를 설정하면 컴포넌트가 다시 렌더링 됩니다. ref를 변경하면 다시 렌더링 되지 않습니다!
7474

75-
## 예시: 스톱워치 작성 {/*example-building-a-stopwatch*/}
75+
## 예시: 스톱워치 작성하기 {/*example-building-a-stopwatch*/}
7676

7777
ref와 state를 단일 컴포넌트로 결합할 수 있습니다. 예를 들어 사용자가 버튼을 눌러 시작하거나 중지할 수 있는 스톱워치를 만들어봅시다. 사용자가 "시작"을 누른 후 시간이 얼마나 지났는지 표시하려면 시작 버튼을 누른 시기와 현재 시각을 추적해야 합니다. **이 정보는 렌더링에 사용되므로 state를 유지합니다.**
7878

@@ -93,12 +93,12 @@ export default function Stopwatch() {
9393
const [now, setNow] = useState(null);
9494

9595
function handleStart() {
96-
// Start counting.
96+
// 카운팅을 시작합니다.
9797
setStartTime(Date.now());
9898
setNow(Date.now());
9999

100100
setInterval(() => {
101-
// Update the current time every 10ms.
101+
// 10ms 마다 현재 시간을 업데이트 합니다.
102102
setNow(Date.now());
103103
}, 10);
104104
}
@@ -121,7 +121,7 @@ export default function Stopwatch() {
121121

122122
</Sandpack>
123123

124-
"Stop" 버튼을 누르면 `now` state 변수의 업데이트를 중지하기 위해 기존 interval을 취소해야 합니다. 이를 위해 [`clearInterval`](https://developer.mozilla.org/en-US/docs/Web/API/clearInterval)을 호출하면 됩니다. 그러나 이전에 사용자가 시작을 눌렀을 때 `setInterval` 호출로 반환된 interval ID를 제공해야 합니다. interval ID는 어딘가에 보관해야 합니다. **interval ID는 렌더링에 사용되지 않으므로 ref에 보관할 수 있습니다.**
124+
"Stop" 버튼을 누르면 `now` state 변수의 업데이트를 중지하기 위해 기존 interval을 취소해야 합니다. 이를 위해 [`clearInterval`](https://developer.mozilla.org/en-US/docs/Web/API/clearInterval)을 호출하면 됩니다. 그러나 이전에 사용자가 시작을 눌렀을 때 `setInterval` 호출로 반환된 interval ID를 제공해야 합니다. interval ID는 어딘가에 보관해야 합니다. **interval ID는 렌더링에 사용되지 않으므로 ref에 저장할 수 있습니다.**
125125

126126
<Sandpack>
127127

@@ -168,18 +168,18 @@ export default function Stopwatch() {
168168

169169
</Sandpack>
170170

171-
렌더링에 정보를 사용할 때 해당 정보를 state로 유지합니다. event handler에게만 정보가 필요하고 변경할 필요가 없을 때, ref를 사용하는 것이 더 효율적일 수 있습니다.
171+
렌더링에 정보를 사용할 때 해당 정보를 state로 유지합니다. event handler에게만 필요한 정보이고 변경이 일어날 때 리렌더가 필요하지 않다면, ref를 사용하는 것이 더 효율적일 수 있습니다.
172172

173173
## ref와 state의 차이 {/*differences-between-refs-and-state*/}
174174

175-
예를 들어, ref가 state보다 덜 "엄격한" 것으로 생각될 수 있습니다-항상 state 설정 함수를 사용하지 않고 변경할 수 있습니다. 하지만 대부분은 state를 사용하고 싶을 것입니다. ref는 자주 필요하지 않은 "escape hatch"입니다. state 및 ref의 비교 방법은 다음과 같습니다.
175+
ref가 state보다 덜 "엄격한" 것으로 생각될 수 있습니다-예를 들어, 항상 state 설정 함수를 사용하지 않고 변경할 수 있습니다. 하지만 대부분은 state를 사용하고 싶을 것입니다. ref는 자주 필요하지 않은 "escape hatch"입니다. state와 ref를 비교한 것은 다음과 같습니다.
176176

177-
| refs | state |
178-
| ------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
179-
| `useRef(initialValue)` `{ current: initialValue }` 을 반환합니다. | `useState(initialValue)` 은 state 변수와 setter 함수 `[value, setValue]` 의 current 값을 반환합니다. |
180-
| state를 바꿔도 리렌더 되지 않습니다. | state를 바꾸면 리렌더됩니다. |
181-
| Mutable-렌더링 프로세스 외부에서 `current` 값을 수정 및 업데이트할 수 있습니다. | "Immutable"—state 설정 함수를 사용하여 state 변수를 수정하여 리렌더러를 대기열에 넣어야 합니다. |
182-
| 렌더링 중에는 `current` 값을 읽거나 쓰면 안 됩니다. | 언제든지 state를 읽을 수 있습니다. 그러나 각 렌더에는 변경되지 않는 자체적인 [snapshot](/learn/state-as-a-snapshot) state가 있습니다.
177+
| refs | state |
178+
|---------------------------------------------------------------|----------------------------------------------------------------------------------------------------|
179+
| `useRef(initialValue)` `{ current: initialValue }` 을 반환합니다. | `useState(initialValue)` 은 state 변수의 현재 값과 setter 함수 `[value, setValue]` 반환합니다. |
180+
| state를 바꿔도 리렌더 되지 않습니다. | state를 바꾸면 리렌더 됩니다. |
181+
| Mutable-렌더링 프로세스 외부에서 `current` 값을 수정 및 업데이트할 수 있습니다. | "Immutable"—state 를 수정하기 위해서는 state 설정 함수를 반드시 사용하여 리렌더 대기열에 넣어야 합니다. |
182+
| 렌더링 중에는 `current` 값을 읽거나 쓰면 안 됩니다. | 언제든지 state를 읽을 수 있습니다. 그러나 각 렌더마다 변경되지 않는 자체적인 state의 [snapshot](/learn/state-as-a-snapshot)있습니다.
183183

184184
다음은 state와 함께 구현되는 카운터 버튼입니다.
185185

@@ -218,7 +218,7 @@ export default function Counter() {
218218
let countRef = useRef(0);
219219

220220
function handleClick() {
221-
// This doesn't re-render the component!
221+
// 이것은 컴포넌트의 리렌더를 일으키지 않습니다!
222222
countRef.current = countRef.current + 1;
223223
}
224224

@@ -236,7 +236,7 @@ export default function Counter() {
236236

237237
<DeepDive>
238238

239-
#### How does useRef work inside? {/*how-does-useref-work-inside*/}
239+
#### useRef는 내부적으로 어떻게 동작하나요? {/*how-does-useref-work-inside*/}
240240

241241
`useState``useRef`가 모두 React에 의해 제공되지만, 원칙적으로 `useRef``useState` 위에 구현될 수 있습니다. React 내부에서 `useRef`가 이렇게 구현되는 것을 상상할 수 있습니다.
242242

@@ -250,7 +250,7 @@ function useRef(initialValue) {
250250

251251
첫 번째 렌더 중에 `useRef``{ current: initialValue }`을 반환합니다. 이 객체는 React에 의해 저장되므로 다음 렌더 중에 같은 객체가 반환됩니다. 이 예시에서는 state setter가 어떻게 사용되지 않는지 주의하세요. `useRef`는 항상 동일한 객체를 반환해야 하므로 필요하지 않습니다!
252252

253-
React는 `useRef`가 실제로 충분히 일반적이기 때문에 built-in 버전을 제공합니다. 하지만 setter가 없는 정규 state 변수라고 생각하면 됩니다. 객체 지향 프로그래밍에 익숙한 경우 refs는 인스턴스 필드를 상기시킬 수 있습니다-하지만 `this.something` 대신에 `somethingRef.current` 처럼 써야 합니다.
253+
React는 `useRef`가 실제로 충분히 일반적이기 때문에 built-in 버전을 제공합니다. setter가 없는 일반적인 state 변수라고 생각할 수 있습니다. 객체 지향 프로그래밍에 익숙하다면 refs는 인스턴스 필드를 상기시킬 수 있습니다-하지만 `this.something` 대신에 `somethingRef.current` 처럼 써야 합니다.
254254

255255
</DeepDive>
256256

@@ -280,11 +280,11 @@ console.log(ref.current); // 5
280280

281281
그 이유는 **ref 자체가 일반 자바스크립트 객체**처럼 동작하기 때문입니다.
282282

283-
또한 ref로 작업할 때 [mutation 방지](/learning/updating-objects-in-state)에 대해 걱정할 필요가 없습니다. 변형하는 객체가 렌더링에 사용되지 않는 한, React는 ref 혹은 해당 콘텐츠를 어떻게 처리하든 신경 쓰지 않습니다.
283+
또한 ref로 작업할 때 [mutation 방지](/learn/updating-objects-in-state)에 대해 걱정할 필요가 없습니다. 변형하는 객체가 렌더링에 사용되지 않는 한, React는 ref 혹은 해당 콘텐츠를 어떻게 처리하든 신경 쓰지 않습니다.
284284

285285
## Refs 와 DOM {/*refs-and-the-dom*/}
286286

287-
임의의 값을 ref로 지정할 수 있습니다. 그러나 ref의 가장 일반적인 사용 사례는 DOM 엘리먼트에 액세스하는 것입니다. 예를 들어 프로그래밍 방식으로 입력의 초점을 맞추려는 경우 유용합니다. `<div ref={myRef}>`와 같은 JSX의 `ref` 어트리뷰트에 ref를 전달하면 React는 해당 DOM 엘리먼트를 `myRef.current`에 넣습니다. 이에 대한 자세한 내용은 [Refs를 사용하여 DOM 조작](/learn/maniping-the-dom-with-refs)에서 확인할 수 있습니다.
287+
임의의 값을 ref로 지정할 수 있습니다. 그러나 ref의 가장 일반적인 사용 사례는 DOM 엘리먼트에 액세스하는 것입니다. 예를 들어 프로그래밍 방식으로 입력의 초점을 맞추려는 경우 유용합니다. `<div ref={myRef}>`와 같은 JSX의 `ref` 어트리뷰트에 ref를 전달하면 React는 해당 DOM 엘리먼트를 `myRef.current`에 넣습니다. 이에 대한 자세한 내용은 [Refs를 사용하여 DOM 조작](/learn/manipulating-the-dom-with-refs)에서 확인할 수 있습니다.
288288

289289
<Recap>
290290

@@ -412,7 +412,7 @@ export default function Chat() {
412412

413413
#### 리렌더를 못하는 컴포넌트 수정 {/*fix-a-component-failing-to-re-render*/}
414414

415-
이 버튼은 "On"과 "Off"를 표시하게 되어 있습니다. 그러나 항상 "Off"로 표시됩니다. 코드가 뭐가 잘못됐나요? 고쳐볼게요.
415+
이 버튼은 "On"과 "Off"를 표시하게 되어 있습니다. 그러나 항상 "Off"로 표시됩니다. 코드가 뭐가 잘못됐나요? 고쳐봅시다.
416416

417417
<Sandpack>
418418

@@ -466,7 +466,7 @@ export default function Toggle() {
466466

467467
예시는 작동하지만, 의도한 대로 작동하지 않습니다. 버튼은 독립적이지 않습니다. 문제를 보려면 버튼 중 하나를 클릭한 다음 즉시 다른 버튼을 클릭합니다. 지연된 후에 양쪽 버튼의 메시지를 볼 수 있을 것이라고 예상할 것입니다. 그러나 마지막 버튼의 메시지만 표시됩니다. 첫 번째 버튼의 메시지가 사라집니다.
468468

469-
왜 두 버튼이 서로 간섭하는 것일까요? 문제를 찾아 해결해보죠.
469+
왜 두 버튼이 서로 간섭하는 것일까요? 문제를 찾아 해결해 봅시다.
470470

471471
<Hint>
472472

@@ -482,38 +482,38 @@ import { useState } from 'react';
482482
let timeoutID;
483483

484484
function DebouncedButton({ onClick, children }) {
485-
return (
486-
<button onClick={() => {
487-
clearTimeout(timeoutID);
488-
timeoutID = setTimeout(() => {
489-
onClick();
490-
}, 1000);
491-
}}>
492-
{children}
493-
</button>
494-
);
485+
return (
486+
<button onClick={() => {
487+
clearTimeout(timeoutID);
488+
timeoutID = setTimeout(() => {
489+
onClick();
490+
}, 1000);
491+
}}>
492+
{children}
493+
</button>
494+
);
495495
}
496496

497497
export default function Dashboard() {
498-
return (
499-
<>
500-
<DebouncedButton
501-
onClick={() => alert('Spaceship launched!')}
502-
>
503-
Launch the spaceship
504-
</DebouncedButton>
505-
<DebouncedButton
506-
onClick={() => alert('Soup boiled!')}
507-
>
508-
Boil the soup
509-
</DebouncedButton>
510-
<DebouncedButton
511-
onClick={() => alert('Lullaby sung!')}
512-
>
513-
Sing a lullaby
514-
</DebouncedButton>
515-
</>
516-
)
498+
return (
499+
<>
500+
<DebouncedButton
501+
onClick={() => alert('Spaceship launched!')}
502+
>
503+
Launch the spaceship
504+
</DebouncedButton>
505+
<DebouncedButton
506+
onClick={() => alert('Soup boiled!')}
507+
>
508+
Boil the soup
509+
</DebouncedButton>
510+
<DebouncedButton
511+
onClick={() => alert('Lullaby sung!')}
512+
>
513+
Sing a lullaby
514+
</DebouncedButton>
515+
</>
516+
)
517517
}
518518
```
519519

@@ -533,39 +533,39 @@ button { display: block; margin: 10px; }
533533
import { useState, useRef } from 'react';
534534

535535
function DebouncedButton({ onClick, children }) {
536-
const timeoutRef = useRef(null);
537-
return (
538-
<button onClick={() => {
539-
clearTimeout(timeoutRef.current);
540-
timeoutRef.current = setTimeout(() => {
541-
onClick();
542-
}, 1000);
543-
}}>
544-
{children}
545-
</button>
546-
);
536+
const timeoutRef = useRef(null);
537+
return (
538+
<button onClick={() => {
539+
clearTimeout(timeoutRef.current);
540+
timeoutRef.current = setTimeout(() => {
541+
onClick();
542+
}, 1000);
543+
}}>
544+
{children}
545+
</button>
546+
);
547547
}
548548

549549
export default function Dashboard() {
550-
return (
551-
<>
552-
<DebouncedButton
553-
onClick={() => alert('Spaceship launched!')}
554-
>
555-
Launch the spaceship
556-
</DebouncedButton>
557-
<DebouncedButton
558-
onClick={() => alert('Soup boiled!')}
559-
>
560-
Boil the soup
561-
</DebouncedButton>
562-
<DebouncedButton
563-
onClick={() => alert('Lullaby sung!')}
564-
>
565-
Sing a lullaby
566-
</DebouncedButton>
567-
</>
568-
)
550+
return (
551+
<>
552+
<DebouncedButton
553+
onClick={() => alert('Spaceship launched!')}
554+
>
555+
Launch the spaceship
556+
</DebouncedButton>
557+
<DebouncedButton
558+
onClick={() => alert('Soup boiled!')}
559+
>
560+
Boil the soup
561+
</DebouncedButton>
562+
<DebouncedButton
563+
onClick={() => alert('Lullaby sung!')}
564+
>
565+
Sing a lullaby
566+
</DebouncedButton>
567+
</>
568+
)
569569
}
570570
```
571571

@@ -589,26 +589,26 @@ button { display: block; margin: 10px; }
589589
import { useState, useRef } from 'react';
590590

591591
export default function Chat() {
592-
const [text, setText] = useState('');
593-
594-
function handleSend() {
595-
setTimeout(() => {
596-
alert('Sending: ' + text);
597-
}, 3000);
598-
}
599-
600-
return (
601-
<>
602-
<input
603-
value={text}
604-
onChange={e => setText(e.target.value)}
605-
/>
606-
<button
607-
onClick={handleSend}>
608-
Send
609-
</button>
610-
</>
611-
);
592+
const [text, setText] = useState('');
593+
594+
function handleSend() {
595+
setTimeout(() => {
596+
alert('Sending: ' + text);
597+
}, 3000);
598+
}
599+
600+
return (
601+
<>
602+
<input
603+
value={text}
604+
onChange={e => setText(e.target.value)}
605+
/>
606+
<button
607+
onClick={handleSend}>
608+
Send
609+
</button>
610+
</>
611+
);
612612
}
613613
```
614614

@@ -624,32 +624,32 @@ state는 [snapshot 처럼](/learn/state-as-a-snapshot) 작동하므로 타임아
624624
import { useState, useRef } from 'react';
625625

626626
export default function Chat() {
627-
const [text, setText] = useState('');
628-
const textRef = useRef(text);
629-
630-
function handleChange(e) {
631-
setText(e.target.value);
632-
textRef.current = e.target.value;
633-
}
634-
635-
function handleSend() {
636-
setTimeout(() => {
637-
alert('Sending: ' + textRef.current);
638-
}, 3000);
639-
}
640-
641-
return (
642-
<>
643-
<input
644-
value={text}
645-
onChange={handleChange}
646-
/>
647-
<button
648-
onClick={handleSend}>
649-
Send
650-
</button>
651-
</>
652-
);
627+
const [text, setText] = useState('');
628+
const textRef = useRef(text);
629+
630+
function handleChange(e) {
631+
setText(e.target.value);
632+
textRef.current = e.target.value;
633+
}
634+
635+
function handleSend() {
636+
setTimeout(() => {
637+
alert('Sending: ' + textRef.current);
638+
}, 3000);
639+
}
640+
641+
return (
642+
<>
643+
<input
644+
value={text}
645+
onChange={handleChange}
646+
/>
647+
<button
648+
onClick={handleSend}>
649+
Send
650+
</button>
651+
</>
652+
);
653653
}
654654
```
655655

0 commit comments

Comments
 (0)