[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);
相关推荐
烛阴2 小时前
TypeScript高手密技:解密类型断言、非空断言与 `const` 断言
前端·javascript·typescript
Orange3015116 小时前
《深入源码理解webpack构建流程》
前端·javascript·webpack·typescript·node.js·es6
Ratten10 小时前
03.TypeScript 常见泛型工具详解
typescript
烛阴14 小时前
TypeScript 函数重载入门:让你的函数签名更精确
前端·javascript·typescript
随笔记14 小时前
react中函数式组件和类组件有什么区别?新建的react项目用函数式组件还是类组件?
前端·react.js·typescript
葡萄城技术团队14 小时前
TypeScript 进阶必备!5 个实用工具类型,帮你写出更健壮的前端代码
typescript
定栓15 小时前
Typescript入门-对象讲解
前端·javascript·typescript
ssshooter1 天前
VSCode 自带的 TS 版本可能跟项目TS 版本不一样
前端·面试·typescript
青衫客361 天前
用 Python 实现一个“小型 ReAct 智能体”:思维链 + 工具调用 + 环境交互
python·大模型·llm·react
Lsx_1 天前
TypeScript 是怎么去查找类型定义的?
前端·javascript·typescript