以antd为例 React+Typescript 引入第三方UI库

本文 我们来说说 第三方UI库

其实应用市场上的 第三方UI库都是非常优秀的

那么 react 我们比较熟的肯定还是 antd 我们还是来用它作为演示

这边 我们先访问他的官网 https://3x.ant.design/index-cn

点击开始使用

在左侧 有一个 在 TypeScript 中使用

通过图标我们也可以看出 这个UI库与react的关系不一般

上面这种 快速创建一个项目的 就算了 不太适合我们的情况

我们看下面引入的方式

这里 我们还是用 npm的方式

打开我们的项目 终端输入

java 复制代码
npm install antd --save

这样 依赖包就进来了

然后 我们

javascript 复制代码
npm start

启动项目

这边也是没有任何问题

然后 我们按这个文档的案例 将自己的组件改一改

typescript 复制代码
import * as React from "react";
import Button from 'antd/es/button';

interface IProps {
}


export default class hello extends React.Component<IProps,any> {

    public readonly state: Readonly<any> = {
        data: []
    }
    
    public constructor(props:IProps){
        super(props);
    }

    public render() {
        return (
            <div>
              <Button type="primary">Button</Button>
            </div>
        )
    }
}

运行项目

按钮就出现了

然后 我们尝试一个其他组件

编写代码如下

typescript 复制代码
import * as React from "react";
import { Progress } from 'antd';

interface IProps {
}

export default class hello extends React.Component<IProps,any> {

    public readonly state: Readonly<any> = {
        data: []
    }
    
    public constructor(props:IProps){
        super(props);
    }

    public render() {
        return (
            <div>
                <Progress type="circle" percent={75} />
                <Progress type="circle" percent={70} status="exception" />
                <Progress type="circle" percent={100} />
            </div>
        )
    }
}

运行结果如下

相关推荐
YAwu1120 小时前
深入解析 React 炫彩鼠标跟随标题组件:从坐标定位到动画性能
前端·react.js
Ruihong1 天前
🎉 VuReact 1.9.0 发布,支持 Vue 3.4 defineModel 编译到 React
vue.js·react.js·面试
spmcor1 天前
React 架构师之路:Next.js 全栈革命(第八篇)
前端·react.js
假如让我当三天老蒯1 天前
React基础、进阶(学习用)
前端·react.js·面试
spmcor1 天前
为什么页面越用越卡?——React组件内存泄漏的排查与修复
react.js
天蓝色的鱼鱼2 天前
React Router v8 来了:react-router-dom 没了,老项目该怎么迁移?
前端·react.js
Bolt2 天前
TypeScript 7.0 来了:当 tsc 用 Go 重写之后
javascript·typescript·go
无名氏同学2 天前
React 16-19 新特性
react.js
写代码的皮筏艇2 天前
React中的forwardRef
前端·react.js·面试
Flynt2 天前
装上TypeScript 7.0 RC之后,最让我意外不是10倍提速
typescript·visual studio code