主页

流行的开源React UI组件库

material-ui(国外)

  1. 官网: http://www.material-ui.com/#/
  2. Github: https://github.com/callemall/material-ui

ant-design(国内蚂蚁金服)

  1. 官网: https://ant.design/index-cn
  2. Github: https://github.com/ant-design/ant-design/

ant-design 组件库使用

安装 npm i antd

引入需要使用的组件 import { Button } from 'antd'

接着引入 css 文件 import 'antd/dist/antd.min.css'

使用 Icon 图标需要安装 npm i @ant-design/icons 图标组件包

antd v3 样式按需引入

文档 https://3x.ant.design/docs/react/use-with-create-react-app-cn

  1. 引入 react-app-rewired 并修改 package.json 里的启动配置。由于新的 react-app-rewired@2.x 版本的关系,你还需要安装 customize-cra

    yarn add react-app-rewired customize-cra
/* package.json */
"scripts": {
-   "start": "react-scripts start",
+   "start": "react-app-rewired start",
-   "build": "react-scripts build",
+   "build": "react-app-rewired build",
-   "test": "react-scripts test",
+   "test": "react-app-rewired test",
}
  1. 然后在项目根目录创建一个 config-overrides.js 用于修改默认配置。

    // 配置具体的修改规则
    module.exports = function override(config, env) {
     // congif 原有的配置
     // do stuff with the webpack config...
     return config;
    };
  2. 使用 babel-plugin-import

    注意:antd 默认支持基于 ES module 的 tree shaking,js 代码部分不使用这个插件也会有按需加载的效果。
    babel-plugin-import 是一个用于按需加载组件代码和样式的 babel 插件(原理),现在我们尝试安装它并修改 config-overrides.js 文件。
    yarn add babel-plugin-import
// config-overrides.js
+ const { override, fixBabelImports } = require('customize-cra');

- module.exports = function override(config, env) {
-   // do stuff with the webpack config...
-   return config;
- };
+ module.exports = override(
+   fixBabelImports('import', {
+     libraryName: 'antd',
+     libraryDirectory: 'es',
+     style: 'css',
+   }),
+ );

然后移除前面在 src/App.css 里全量添加的 @import '~antd/dist/antd.css'; 样式代码,并且按下面的格式引入模块。

  // src/App.js
  import React, { Component } from 'react';
- import Button from 'antd/es/button';
+ import { Button } from 'antd';
  import './App.css';

  class App extends Component {
    render() {
      return (
        <div className="App">
          <Button type="primary">Button</Button>
        </div>
      );
    }
  }

  export default App;
最后重启 yarn start 访问页面,antd 组件的 js 和 css 代码都会按需加载

antd 自定义主题

按照 配置主题 的要求,自定义主题需要用到 less 变量覆盖功能。我们可以引入 customize-cra 中提供的 less 相关的函数 addLessLoader 来帮助加载 less 样式,同时修改 config-overrides.js 文件如下。

yarn add less less-loader
// config-overrides.js
- const { override, fixBabelImports } = require('customize-cra');
+ const { override, fixBabelImports, addLessLoader } = require('customize-cra');

module.exports = override(
  fixBabelImports('import', {
    libraryName: 'antd',
    libraryDirectory: 'es',
-   style: 'css',
+   style: true,
  }),
+ addLessLoader({
    // 允许使用 js 修改 less 文件
+   javascriptEnabled: true,
    // 要修改的变量
+   modifyVars: { '@primary-color': '#1DA57A' },
+ }),
);

这里利用了 less-loader 的 modifyVars 来进行主题配置,变量和其他配置方式可以参考 配置主题 文档。
修改后重启 yarn start,如果看到一个绿色的按钮就说明配置成功了。

你也可以使用 cracocraco-antd 来实现和 customize-cra 一样的修改 create-react-app 配置的功能。

antd 的按需引入+自定主题

  1. 安装依赖:yarn add react-app-rewired customize-cra babel-plugin-import less less-loader
  2. 修改package.json

    "scripts": {
     "start": "react-app-rewired start",
     "build": "react-app-rewired build",
     "test": "react-app-rewired test",
     "eject": "react-scripts eject"
    },
  3. 根目录下创建 config-overrides.js

    //配置具体的修改规则
    const { override, fixBabelImports,addLessLoader} = require('customize-cra');
    module.exports = override(
     fixBabelImports('import', {
     libraryName: 'antd',
     libraryDirectory: 'es',
     style: true,
     }),
     addLessLoader({
         lessOptions:{
             javascriptEnabled: true,
             modifyVars: { '@primary-color': 'green' },
         }
     }),
    );
  4. 备注:不用在组件里亲自引入样式了,即:import 'antd/dist/antd.css' 应该删掉

React

版权属于:Joe
作品采用:本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
0

目录

来自 《React-UI 组件库》
评论

qiaofugui

博主很懒,啥都没有
188 文章数
14 评论量
3 分类数
191 页面数
已在风雨中度过 2年138天13小时4分