[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);
相关推荐
Kisang.14 小时前
【HarmonyOS】性能优化——组件的封装与复用
华为·性能优化·typescript·harmonyos·鸿蒙
在未来等你16 小时前
AI Agent设计模式 Day 7:Tree-of-Thoughts模式:树形思维探索
设计模式·llm·react·ai agent·plan-and-execute
U***49831 天前
前端TypeScript教程汇总,从基础到高级
前端·javascript·typescript
百***35511 天前
TypeScript 与后端开发Node.js
javascript·typescript·node.js
宇余1 天前
ES2025新特性实战:5分钟get前端高频实用语法
前端·typescript
一只小阿乐1 天前
react 点击事件注意事项
前端·javascript·react.js·react
网络点点滴2 天前
简单介绍TS中的接口、泛型、自定义类型
typescript
濮水大叔2 天前
VonaJS: 基于winston的Logger日志系统
typescript·nodejs·nestjs
zhenryx3 天前
React Native 自定义 ScrollView 滚动条:开箱即用的 IndicatorScrollView(附源码示例)
javascript·react native·react.js·typescript