简体   繁体   English

将 Create-React-App 与 antd 结合使用

[英]Use Create-React-App with antd

I tried to use antd with create react app .我尝试将antdcreate react app一起使用。

I installed antd in project using我在项目中安装了 antd 使用

yarn add antd

Added a button using antd's button component使用 antd 的按钮组件添加了一个按钮

import React, { Component } from 'react';
import Button from 'antd/lib/button';
import './App.css';

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

export default App;

Still, button did not rendering correctly.尽管如此,按钮没有正确呈现。 When inspecting the element, I find that antd classes are being applied.检查元素时,我发现正在应用 antd 类。 It's just that css is not loaded.只是没有加载css。

I had to add antd/dist/antd.css at the top of src/App.css.我不得不在 src/App.css 的顶部添加 antd/dist/antd.css。

@import '~antd/dist/antd.css';

.App {
  text-align: center;
}

I figured out this from antd's official docs for using with Create React App我从antd 的官方文档中发现了这一点,用于与 Create React App 一起使用

So all the steps required are to install antd所以所有需要的步骤都是安装antd

yarn add antd

Use a component from antd使用来自 antd 的组件

import React, { Component } from 'react';
import Button from 'antd/lib/button';
import './App.css';

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

export default App;

And import antdcss in main css file (src/App.css)并在主 css 文件 (src/App.css) 中导入 antdcss

Although verified answer works, you could minimize the size of the CSS bundle in the build by only importing required CSS as follows.尽管经过验证的答案有效,但您可以通过仅导入所需的 CSS来最小化构建中 CSS 包的大小,如下所示。

 import Button from 'antd/lib/button'; import 'antd/lib/button/style/css'; // importing only the required CSS

Update更新

Recently noticed even importing antd components according to above mentioned way(strike through text) it still adds unnecessary JS in to the build,最近注意到即使按照上述方式(删除文本)导入 antd 组件,它仍然在构建中添加了不必要的 JS,

I even tried react-app-rewired in ant design docs issue still the same ( still adds unnecessary JS in to the build ).我什至尝试过 react-app-rewired in ant design docs问题仍然相同(仍然在构建中添加了不必要的 JS)。 So I opened an issue on the antd repo : https://github.com/ant-design/ant-design/issues/13274所以我在 antd repo 上开了一个 issue: https : //github.com/ant-design/ant-design/issues/13274

Update 2:更新 2:

Please take a look at : https://github.com/ant-design/ant-design/issues/12011请看一看: https : //github.com/ant-design/ant-design/issues/12011

After installing antd in your project you can import button as在您的项目中安装 antd 后,您可以将按钮导入为

import {Button} from 'antd';从'antd'导入{Button};

First install less-loader on npm via npm i less-loader , then create a index.less folder on the index.js directory.首先通过npm i less-loader less-loader 在 npm 上安装 less-loader ,然后在 index.js 目录下创建一个 index.less 文件夹。 paste this on index.less -> @import "~antd/dist/antd.dark.less";将此粘贴到 index.less -> @import "~antd/dist/antd.dark.less"; After that import this .less file to your index.js and have fun :) You can find some other methods here之后,将这个 .less 文件导入到你的 index.js 并玩得开心:) 你可以在这里找到一些其他的方法

You can also use craco(create react app config override) but its more complicated.您也可以使用 craco(创建反应应用配置覆盖),但它更复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM