[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);
相关推荐
cz追天之路17 小时前
华为机考 ------ 识别有效的IP地址和掩码并进行分类统计
javascript·华为·typescript·node.js·ecmascript·less·css3
星光不问赶路人1 天前
TypeScript 架构实践:从后端接口到 UI 渲染数据流的完整方案
前端·vue.js·typescript
码界奇点1 天前
基于React与TypeScript的后台管理系统设计与实现
前端·c++·react.js·typescript·毕业设计·源代码管理
CodeCaptain1 天前
一个快速校验地图资源是否符合兼容要求的小脚本(Cocos Creator3.8.0)
游戏·typescript·cocos2d
树叶会结冰1 天前
TypeScript---循环:要学会原地踏步,更要学会跳出舒适圈
前端·javascript·typescript
没事多睡觉6661 天前
零基础React + TypeScript 教程
前端·react.js·typescript
wordbaby2 天前
TypeScript 类型断言和类型注解的区别
前端·typescript
天天向上vir2 天前
防抖与节流
前端·typescript·vue
打小就很皮...2 天前
发送到飞书机器人的完整流程(拓展)
react·markdown·webhook·飞书机器人
AI前端老薛3 天前
TypeScript 内置工具类型全解析
typescript