팀원이 쓴 코드에 forwarding refs가 적용되어 있었다.
이해할 수 없는 코드였기에 공부를 하면 이곳에 정리(번역)하고자 한다.
이씨.. 글이 엄청 기네
function FancyButton(props) {
return (
<button className="FancyButton">
{props.children}
</button>
);
}
const FancyButton = React.forwardRef((props, ref) => (
<button ref={ref} className="FancyButton">
{props.children}
</button>
));
// You can now get a ref directly to the DOM button:
const ref = React.createRef();
<FancyButton ref={ref}>Click me!</FancyButton>;
이런 방식으로 FancyButton은 DOM node인 button에 ref를 전달하고 사용할 수 있다.
단계별로 설명 하자면 아래와 같다
ref를 꼭 DOM component에 한정할 필요는 없다. 당신이 만든 component에도 쓸 수 있다.