如何解决在React中使用forwardRef或仅将ref作为prop传递给它的影响与对useImperitaveHandle的影响之间的区别
React文档为forwardRef提供以下示例
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>;
但是当ref作为prop传递而不是通过ForwardRef传递时,这段代码也可以工作
const FancyButton = (props) => (
<button ref={props.testRef} className="FancyButton">
{props.children}
</button>
));
// You can now get a ref directly to the DOM button:
const ref = React.createRef();
<FancyButton testRef={ref}>Click me!</FancyButton>;
如果这两种方式都相同,那么使用ForwardRef的原因是什么?
当ref作为道具传递时,还可以使用ImperitaveHandle Hook吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。