forwarded ref에서 .current 사용하는 법 Typescript + React

JJVoiture·2023년 3월 9일
0

TypescriptforwardRef으로 상속 받은 ref에 대해서는 ref.current를 사용 시 typescript에서 아래와 같은 문제를 반환한다.

Property 'current' does not exist on type '((instance: HTMLInputElement | null) => void) | MutableRefObject<HTMLInputElement | null>'.
Property 'current' does not exist on type '(instance: HTMLInputElement | null) => void'.

해결 방법

type '((instance: HTMLInputElement | null) => void) | MutableRefObject<HTMLInputElement | null>' 니까

typeof ref !== "function"

라는 조건문을 넣으면 ref가 MutableRefObject이므로 typescript에서 오류로 인식하지 않게 된다.

profile
안녕하세요.

0개의 댓글