⚠️
리액트 19버전부터 적용됨.
목적
- 보일러 플레이트 코드 감소
- 아키텍처 간소화
배경
- react 19이전에는 ref를 클래스 컴포넌트와 DOM 요소에서만 사용할 수 있었음.
- 따라서, 함수형 컴포넌트에서는 ref를 받을수 없어서, forwardRef로 명시적으로 ref를 자식이나 DOM 노드에 전달했음.
비교
기존
import React, { forwardRef } from 'react';
const MyButton = forwardRef((props, ref) => (
<button ref={ref} {...props}>
{props.children}
</button>
));
// 사용 예시
const App = () => {
const buttonRef = React.useRef();
return <MyButton ref={buttonRef}>Click Me</MyButton>;
};
변경 후
const MyButton = ({ ref, ...props }) => {
return (
<button ref={ref} {...props}>
{props.children}
</button>
);
};
// 사용 예시
const App = () => {
const buttonRef = React.useRef();
return <MyButton ref={buttonRef}>Click Me</MyButton>;
};
참고 게시글
[번역] 리액트 19 forwardRef 지원 중단: 앞으로 ref를 전달하기 위한 표준 가이드
원본: https://imrankhani.medium.com/react-19-deprecated-forwardref-a-guide-to-passing-ref-as-a-standard-prop-7c0f13e6a229
siosio3103.medium.com
'React' 카테고리의 다른 글
CRA 없이 리액트 프로젝트 생성하기 (Create React Project Without CRA) (4) | 2023.12.13 |
---|---|
React Hook 알아보기 - useMemo() (0) | 2023.08.21 |
react-router-dom v6 설정하기 (0) | 2023.07.24 |