Typescript RefForwardingComponent not working

后端 未结 4 1598
天命终不由人
天命终不由人 2021-01-19 05:03

I am trying to make my component accept a ref.

I have a component like so:

const MyComponent: RefForwardingCom         


        
4条回答
  •  天涯浪人
    2021-01-19 05:11

    Your code is right but you are missing a small detail.

    When you use RefForwardingComponent you need to export the component wrapped with forwardRef

    import React, { forwardRef, RefForwardingComponent } from 'react';
    
    type IMyComponentProps = {}
    const MyComponent: RefForwardingComponent = (props, ref) => {
        return 
    Hoopla
    } export default forwardRef(MyComponent);

提交回复
热议问题