[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);
相关推荐
dancing9994 小时前
cocos3.X的oops框架oops-plugin-excel-to-json改进兼容多表单导出功能
前端·javascript·typescript·游戏程序
yinke小琪10 小时前
快速开始 - TypeScript 入门指南
前端·typescript
wordbaby14 小时前
🎯 satisfies 关键字详解(TypeScript)
前端·typescript
SailingCoder18 小时前
grafana-mcp-analyzer:基于 MCP 的轻量 AI 分析监控图表的运维神器!
运维·人工智能·typescript·node.js·grafana
狂炫一碗大米饭1 天前
一文打通TypeScript 泛型
前端·javascript·typescript
whatever who cares1 天前
React hook之userReducer
react.js·react
Dignity_呱2 天前
别在傻傻分不清any void never unknown的场景啦
前端·vue.js·typescript
aiguangyuan2 天前
React Hooks 基础指南
react·前端开发
烛阴2 天前
模块/命名空间/全局类型如何共存?TS声明空间终极生存指南
前端·javascript·typescript
漫谈网络2 天前
TypeScript 编译 ES6+ 语法到兼容的 JavaScript介绍
javascript·typescript·es6