ref 관련 질문드립니다
안녕하세요 강의 수강 중 궁금한 점이 생겨 질문드립니다
1. function useRef<T>(initialValue : T | null) : RefObject<T>;
2. function useRef<T>(initialValue : T ) : MutableRefObject<T>;
3. function useRef<T = undefined>() : MutableRefObject<T | undefined>;
setTimeout, useRef 타이핑 강의 끝부분에서
<T>를 <number | null> 을 줬습니다.
그러면 2번 꼴이 만들어지는 거를 확인할 수 있었는데요,
그럼, <number> 만 잇엇을때에도 똑같이 Mutable~ 로 인식할수잇다고 생각햇는데,
제가 어디를 놓친건지 알려주시면 감사하겟습니다
답변 2
4
useRef returns a mutable ref object whose .current property is initialized to the passed argument (initialValue). The returned object will persist for the full lifetime of the component.
Note that useRef() is useful for more than the ref attribute. It’s handy for keeping any mutable value around similar to how you’d use instance fields in classes.
Usage note: if you need the result of useRef to be directly mutable, include | null in the type of the generic argument.
공식문서의 설명인데 좀 더 보충해보겠습니다.
useRef<number>(null)의 경우를 생각해봅시다.
T를 number로 잡으면 initialValue는 null이므로 2번이 될 수 없습니다. 그래서 1번 RefObject가 됩니다.
1번에서는 T가 number여도 initialValue는 number | null이라서 null이 될 수 있습니다.
number | null을 T로 잡고 initialValue에 null을 넣으면 initialValue인 null은 T에 부합합니다. (T === number | null이므로) 따라서 2번이 됩니다.
express response 타입
0
210
1
createRoot
0
285
1
babel-loader질문입니다! 왜쓰는지 궁금합니다!
0
685
1
undefined 처리
0
493
1
compilerOptions lib 관련
0
329
1
event type 찾기
0
324
1
정확한 type vs 가독성
0
292
1
useRef에 대해 질문드립니다.
0
305
1
tsx파일에서 에러 표시
0
864
1
Props type 질문드립니다.
0
227
1
webpack.config.ts/ Could not find a declaration file for module '@pmmmwh/react-refresh-webpack-plugin'.
0
590
1
18버전에서의 ReactDOM.render
1
658
1
깃허브에 react-router@6 업데이트 해주신 코드 질문드립니다.
0
294
1
2강 끝말잇기 npx webpack 오류가 질의사항
0
408
1
Property 'render' does not exist on type 'IntrinsicAttributes & (PathRouteProps | LayoutRouteProps | IndexRouteProps)'.
0
716
1
webpack.config.ts import 할 때 에러좀 봐주세요!!
0
307
1
React.FC에 대해 궁금합니다.
0
701
1
가위바위보 interval 타입과 이벤트타입에 관한질문
0
343
1
npm run dev 실행은 성공적으로 되는데
0
677
2
redux 유료강좌를 듣고 와야 되는지 여쭈어보려고 합니다.
0
320
1
git 내용과 강의 내용이 달라서 여쭈어봅니다.
0
223
1
Ball.tsx 타이핑
0
324
2
react-router 버젼 업그레이드 변경사항 문의
0
286
2
리액트 타입스크립트 공식문서
0
338
1





