[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);
相关推荐
康小旺10 小时前
AI 写 Vue 总写成同一个味道:给它装 Skills,装的不是知识是边界
vue.js·typescript
ZoeLandia18 小时前
React08:自定义 Hook
开发语言·前端·react.js·typescript
漫步的猪1 天前
# 告别混乱与付费墙!Linkit:完全免费、本地优先的开源 AI 智能书签与收藏夹管理神器
人工智能·ai·开源·react·免费·书签·收藏夹
aloha_7892 天前
HTML、JavaScript 与 TypeScript 基础速查
javascript·typescript·html
离歌漠2 天前
《我用 Tauri 做了一个本地开发工具箱》
rust·typescript·vue
enjoy编程3 天前
playwright学习-I
typescript·vue·nodejs·1024程序员节·playwright
iDao技术魔方3 天前
Bun v1.4.4 实战:用 bun fmt 替掉 prettier 的 5 个步骤
typescript·1024程序员节
梅雅达编程笔记3 天前
Day 17 | uni-app 小程序:前端从零搭建
微信小程序·typescript·uni-app·vue3·pinia·前端开发·全栈
康小旺3 天前
TypeScript 编译器开始卷 Rust 了:Cloudflare 的 tsrs 到底想干嘛?
rust·typescript
nyaomaru4 天前
is-kit 达成 100 Stars ⭐🎉 从 50 到 100,发生了什么?
前端·typescript