[react 3种方法] 获取ant组件ref用ts如何定义?

获取ant的轮播图组件, 我用ts如何定义?

Strongly Type useRef with ElementRef | Total TypeScript

javascript 复制代码
import React, {  ElementRef } from 'react';
javascript 复制代码
 const lunboRef = useRef<ElementRef<typeof Carousel>>(null);
javascript 复制代码
    <Carousel autoplay ref={lunboRef}>

这样就行了! ,然后点一下看看.弹出提示了

当然你还可以用ant内置的GetRef

Type Util - Ant Design

这个2个一样

javascript 复制代码
    const lunboRef = useRef<GetRef<typeof Carousel>>(null);
    // const lunboRef = useRef<ElementRef<typeof Carousel>>(null);

最后一种

ComponentRef

javascript 复制代码
import React, { useRef, ComponentRef } from 'react';
useRef<ComponentRef<typeof Carousel>>(null);
相关推荐
kidding72320 小时前
前端VUE3的面试题
前端·typescript·compositionapi·fragment·teleport·suspense
来一碗刘肉面2 天前
TypeScript - 属性修饰符
前端·javascript·typescript
Rowrey2 天前
react+typescript,初始化与项目配置
javascript·react.js·typescript
乔冠宇2 天前
微信小程序中将图片截图为正方形(自动居中)
微信小程序·小程序·typescript·uniapp
念九_ysl3 天前
前端循环全解析:JS/ES/TS 循环写法与实战示例
前端·javascript·typescript
MardaWang3 天前
HarmonyOS开发,遇到 Object.assign(this, source)报错怎么解决?
typescript·harmonyos
IT、木易4 天前
TypeScript跟js,es6这些的区别
javascript·typescript·es6
孟陬4 天前
持续改善 React 代码的 SOLID 原则(附带 hooks 详细案例)适用于高级前端
react.js·设计模式·typescript
李二。5 天前
TypeScript学习:初学
typescript
DCTANT5 天前
【原创】vue-element-admin-plus完成编辑页面中嵌套列表功能
前端·javascript·vue.js·elementui·typescript