react 中 如何通过 typeof 获取 组件 ref 返回类型

48 阅读1分钟

需求: 想知道组件实例上暴露了哪些api


export type FabricImgHandle = {
  undo: () => void
  redo: () => void
}

const FabricImg = forwardRef<FabricImgHandle, FCProps>((props, ref) => {
})

 useImperativeHandle(ref, () => ({
      undo: () => {},
      redo: () => {}
    }));
    
  const fabricImgRef = useRef<ElementRef<typeof FabricImg>>()
  fabricImgRef.current?.undo()

参考:stackoverflow.com/questions/6…