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: README.md
+74
Original file line number
Diff line number
Diff line change
@@ -386,3 +386,77 @@ function DelayedEffect(props: { timerMs: number }) {
386
386
```
387
387
388
388
</details>
389
+
390
+
#### useRef
391
+
392
+
TypeScript에서 `useRef`는 type argument가 초기 값을 완전히 포함(cover)하는지 아닌지에 따라[read-only](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/abd69803c1b710db58d511f4544ec1b70bc9077c/types/react/v16/index.d.ts#L1025-L1039)또는 [mutable](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/abd69803c1b710db58d511f4544ec1b70bc9077c/types/react/v16/index.d.ts#L1012-L1023) 둘 중 하나를 반환합니다. 각자의 use case에 맞는 것을 선택하세요.
393
+
394
+
##### Option 1: DOM element ref
395
+
396
+
**[DOM element에 접근하기 위해서는](https://reactjs.org/docs/refs-and-the-dom.html):** element type 만을 argument로 넘겨주고 `null`을 초기 값으로 사용하세요. 이 경우에, 반환되는 reference는 React에 의해 관리되는 read-only `.current`를 가질 것입니다. TypeScript는 이 ref를 element의 `ref` prop으로 전달 받기를 기대합니다. :
397
+
398
+
```tsx
399
+
function Foo() {
400
+
// - 가능한 상세하게 작성하세요. 예를들면, HTMLDivElement는 HTMLElement보다 더 좋고,
401
+
// Element보다는 훨신 더 좋은 선택입니다.
402
+
// - 기술적으로 말하자면, 이것은 RefObject<HTMLDivElement>를 반환합니다.
403
+
const divRef =useRef<HTMLDivElement>(null);
404
+
405
+
useEffect(() => {
406
+
// ref.current가 null일 수 있다는 것을 주의하세요.
407
+
// 이것은 당신이 조건에 따라서 ref된(ref-ed) element를 render하거나
408
+
// 할당하는 것을 잊을 수 있기 때문에 예측할 수 있는 현상입니다.
409
+
if (!divRef.current) throwError("divRef is not assigned");
만약 `divRef.current`가 절대로 null이 아닐것이라는 것을 확신한다면, non-null assertion operator `!`을 사용하는 것도 가능합니다. :
421
+
422
+
```tsx
423
+
const divRef =useRef<HTMLDivElement>(null!);
424
+
// 나중에... 이것이 null 인지 확인할 필요가 없습니다.
425
+
doSomethingWith(divRef.current);
426
+
```
427
+
428
+
당신이 type safety가 보장된다고 미리 가정하고 코드를 작성한다는 것에 주의해야 합니다. 렌더링 과정에서 ref를 element에 할당하는 것을 잊거나, ref된(ref-ed) element가 조건부 렌더링 된다면 runtime error가 발생할 것입니다.
429
+
430
+
<details>
431
+
<summary><b>Tip: 어떤 <code>HTMLElement</code>를 사용할지 선택하기</b></summary>
432
+
433
+
Ref는 명시성(specificity)을 필요로 합니다. 즉, `HTMLElement` 만을 명시하는 것은 충분하지 않다는 말입니다. 만약 당신이 필요한 element type의 이름을 모른다면, [lib.dom.ts](https://github.com/microsoft/TypeScript/blob/v3.9.5/lib/lib.dom.d.ts#L19224-L19343)에서 확인하거나 의도적으로 type error를 발생시키고 language service가 type의 이름을 알려주도록 할 수 있습니다.
**[mutable value를 가지기 위해서는](https://reactjs.org/docs/hooks-faq.html#is-there-something-like-instance-variables):** 원하는 type을 사용하고 초기 값이 완전히 해당 type에 속하는지 확인하세요.
442
+
443
+
```tsx
444
+
function Foo() {
445
+
// 기술적으로 말하자면, 이것은 MutableRefObject<number | null>을 반환합니다.
446
+
const intervalRef =useRef<number|null>(null);
447
+
448
+
// 당신이 직접 ref를 관리합니다. (이것이 MutableRefObject라고 불리는 이유이죠.)
449
+
useEffect(() => {
450
+
intervalRef.current=setInterval(...);
451
+
return () =>clearInterval(intervalRef.current);
452
+
}, []);
453
+
454
+
// ref는 element의 "ref" prop으로 전달되지 않습니다.
455
+
return <buttononClick={/* clearInterval the ref */}>Cancel timer</button>;
456
+
}
457
+
```
458
+
459
+
##### 다음의 자료도 확인해 보세요.
460
+
461
+
- [@rajivpunjabi가 작성한 관련 이슈](https://github.com/typescript-cheatsheets/react/issues/388) - [Playground](https://www.typescriptlang.org/play#code/JYWwDg9gTgLgBAKjgQwM5wE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