리액트 딥다이브(4)
·
카테고리 없음
4. useImperativeHandle부모 컴포넌트가 자식 컴포넌트에게 ref를 전달함으로써 자식 컴포넌트의 특정 함수나 값에 접근할 수 있도록 하는 훅이다.일반적으로 부모는 자식의 내부 기능을 직접 건드릴 수 없지만,useImperativeHandle을 사용하면 특정 기능만 공개할 수 있다.// Child.tsx// 리액트 19버전부터는 forwardRef 생략이 가능하다.export const ChildInput = forwardRef((props, ref) => { const inputRef = useRef(); // 부모에게 노출할 기능을 지정 useImperativeHandle(ref, () => ({ focusInput: () => { inputRef.current.foc..