useRef로 생성된 객체 내에는 current라는 게 들어있는데 current을 읽어 오고 사용을 하는데 이 때 무조건 null check를 해줘야한다.
import React, { useRef } from 'react';
const App = () => {
const h1Ref = useRef<HTMLHeadingElement>(null);
if (current !== null) {
// h1Ref 코드
}
return (
<h1 ref={h1Ref}>App</h1>
)
}
export default App;
출처
[TS]useRef 자세히 알아보기(Typescript, useLayoutEffect)
바닐라 자바스크립트로 DOM 컨트롤을 할 때는 getElementById나 querySelector 등을 이용하여 DOM의 속성을 변경하거나 추가하거나 제거를 해왔습니다. 하지만 리액트에서는 조금 다릅니다. 물론 저 방법
krpeppermint100.medium.com
https://driip.me/7126d5d5-1937-44a8-98ed-f9065a7c35b5
TypeScript React에서 useRef의 3가지 정의와 각각의 적절한 사용법
Type 'MutableRefObject<... | undefined>' is not assignable to type ... 에러 좀 그만 보자!
driip.me
https://linguinecode.com/post/how-to-use-react-useref-with-typescript
How to use React useRef with TypeScript
Make sure to use the useLayoutEffect whenever you’re doing any work with the DOM reference object.
linguinecode.com
'Development > React.js' 카테고리의 다른 글
Next - getServerSideProps (0) | 2022.04.07 |
---|---|
react-hook-form (0) | 2022.04.05 |
Functions are not valid as a React child . (0) | 2022.03.30 |
리엑트 버전 18 에러 (0) | 2022.03.30 |
React Icon (0) | 2022.03.30 |