[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);
相关推荐
能来帮帮蒟蒻吗19 小时前
VUE3 -综合实践(Mock+Axios+ElementPlus)
前端·javascript·vue.js·笔记·学习·ajax·typescript
菜鸟una1 天前
【taro3 + vue3 + webpack4】在微信小程序中的请求封装及使用
前端·vue.js·微信小程序·小程序·typescript·taro
struggle20252 天前
continue通过我们的开源 IDE 扩展和模型、规则、提示、文档和其他构建块中心,创建、共享和使用自定义 AI 代码助手
javascript·ide·python·typescript·开源
Bl_a_ck2 天前
【React】Craco 简介
开发语言·前端·react.js·typescript·前端框架
Bl_a_ck3 天前
开发环境(Development Environment)
开发语言·前端·javascript·typescript·ecmascript
郝开3 天前
扩展:React 项目执行 yarn eject 后的 scripts 目录结构详解
react.js·前端框架·react
菜鸟una3 天前
【layout组件 与 路由镶嵌】vue3 后台管理系统
前端·vue.js·elementui·typescript
郝开3 天前
扩展:React 项目执行 yarn eject 后的 config 目录结构详解
react.js·前端框架·react
浪裡遊4 天前
Typescript中的对象类型
开发语言·前端·javascript·vue.js·typescript·ecmascript
从味书4 天前
安装typescript时,npm install -g typescript报错
javascript·typescript·npm