什么是反应中的转发引用以及我们如何使用它以及我们为什么使用它。?

What is forwarding ref in react and How we use it and why we use it.?

我是 React 新手,我看到了术语(转发 ref)。我非常了解 useRef() 钩子、ref 属性和 useState() 钩子。但是我听了这个术语并且我阅读了有关转发 ref 的文章,但不幸的是我不知道它到底是什么,我们为什么使用它,我们在哪里使用以及它将如何在项目中使用。我正在使用功能组件进行反应,所以我想知道功能组件的用法。任何人都可以帮助我,在这里简单地解释一下。

引用转发是一种通过组件自动将引用传递给其子项的技术。

这有助于创建可重用的组件,例如,如果我们想要获取一个按钮组件并想用它做一些事情,我们可以将引用传递给按钮 DOM 元素。

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>;

参考 https://reactjs.org/docs/forwarding-refs.html