React学习笔记4

笔记(2019.6.18)

使用antd(在create-react-app中使用)

1.安装:npm install antd --save
2.在css文件中引入antd的css:@import '~antd/dist/antd.css';

就像使用Bootstrap一样,当我们需要哪个组件时,我们就可以引入哪个组件:

import { Button } from 'antd';
...
 <Button type="primary">这是antd的按钮</Button>
 ...
按需加载Antd的css
  1. 引入 react-app-rewired 并修改 package.json 里的启动配置。由于新的 react-app-rewired@2.x 版本的关系,你还需要安装 customize-cra。
    npm install react-app-rewired customize-cra

  2. 修改package.json: 修改react-scripts为react-app-rewired
    在这里插入图片描述

  3. 然后在项目根目录创建一个 config-overrides.js 用于修改默认配置。

    module.exports = function override(config, env) {
      // do stuff with the webpack config...
      return config;
    };
  1. 安装babel-plugin-import babel-plugin-import是一个用于按需加载组件代码和样式的babel插件
    npm install babel-plugin-import --save
    修改 config-overrides.js 文件。
     const { override, fixBabelImports } = require('customize-cra');
     module.exports = override(
       fixBabelImports('import', {
         libraryName: 'antd',
         libraryDirectory: 'es',
         style: 'css',
       }),
     );
  1. 然后移除前面在 src/App.css 里全量添加的 @import ‘~antd/dist/antd.css’; 样式代码,并且按下面的格式引入模块。

// src/App.js

  import React, { Component } from 'react';
- import Button from 'antd/lib/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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值