[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);
相关推荐
wuhen_n9 小时前
TypeScript的对象类型:interface vs type
前端·javascript·typescript
还是码字踏实10 小时前
智能体平台Dify的 模型接口与 Agent 架构深度解析
react·抽象工厂模式·function call·模型插件系统
EndingCoder15 小时前
反射和元数据:高级装饰器用法
linux·运维·前端·ubuntu·typescript
We་ct16 小时前
LeetCode 135. 分发糖果:双向约束下的最小糖果分配方案
前端·算法·leetcode·typescript
孟无岐1 天前
【Laya】Byte 二进制数据处理
网络·typescript·游戏引擎·游戏程序·laya
孟无岐1 天前
【Laya】ClassUtils 类反射工具
typescript·游戏引擎·游戏程序·laya
We་ct2 天前
LeetCode 380. O(1) 时间插入、删除和获取随机元素 题解
前端·算法·leetcode·typescript
孟无岐2 天前
【Laya】Ease 缓动函数
typescript·游戏引擎·游戏程序·laya
We་ct2 天前
LeetCode 238. 除了自身以外数组的乘积|最优解详解(O(n)时间+O(1)空间)
前端·算法·leetcode·typescript
踢球的打工仔2 天前
typescript-类的静态属性和静态方法
前端·javascript·typescript