[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);
相关推荐
凡二人11 小时前
Flip-js 优雅的处理元素结构变化的动画(解读)
前端·typescript
烛阴12 小时前
【TS 设计模式完全指南】TypeScript 装饰器模式的优雅之道
javascript·设计模式·typescript
YaeZed12 小时前
TypeScript7(元组)
typescript
闲云野鹤_12 小时前
typeScript学习笔记总结(常用)
typescript
之恒君12 小时前
TypeScript(tsconfig.json - references)
typescript
之恒君1 天前
typescript(tsconfig.json - esModuleInterop)
前端·typescript
guangzan1 天前
VS Code 操作 “Delete unused imports” 时,不删除 React 导入
vscode·typescript·eslint
华仔啊1 天前
别再写 TypeScript enum了!新枚举方式让 bundle 瞬间小20%
javascript·typescript
子兮曰2 天前
🚀彻底掌握异步编程:async/await + Generator 深度解析与20个实战案例
前端·javascript·typescript
知否技术2 天前
别再踩坑了!这份 Vue3+TypeScript 项目教程,赶紧收藏!
前端·typescript