美文网首页
组件ref的使用

组件ref的使用

作者: 2c4419e9dafa | 来源:发表于2020-11-28 13:38 被阅读0次

自己封装的组件,想使用ref,调用内部的方法
1.使用React.forwardRef,包裹组件。

export default React.memo(React.forwardRef(QuestionComments));

2.接收ref属性。


image.png

3.封装外部需要调用的方法。

useImperativeHandle(ref, () => ({
        save: () => {
            commentsForm.submit();
        },
    }), [commentsForm]);

相关文章

网友评论

      本文标题:组件ref的使用

      本文链接:https://www.haomeiwen.com/subject/bviqwktx.html