How to import the antd library for React
Ant Design is an open-source design system developed by the Ant Group, the parent company of various financial and technology companies, including Alibaba, Alipay, Huabei, and MYbank.
The Ant Design library offers various customizable UI components integrated into React applications. These components include forms, tables, modals, navigation menus, and other interface elements, which can help developers create consistent and user-friendly interfaces for their web applications.
Prerequisites
This Answer assumes you already have the following:
Node 8.10 installed on your local development machine
The npm 6.0 registry or yarn 1.12.0 installed on your local development machine
Basic knowledge of HTML, CSS, JavaScript, and React
Installation
We can add antd to our already existing React application by using npm or yarn as follows:
npm install antd
yarn add antd
Usage
Now we can import the required antd components in our React component file. For example, if we want to make use of the Button component, we can import it as follows:
import { Button } from 'antd';
Code example
Code explanation
This example considers a simple react application that shows the basic use of antd components.
Line 2: We import the
DatePicker,Space,Button, andTypographycomponents fromantd.Line 4: We destructure the
Paragraphcomponent fromTypography.Line 8: We use the
Spacecomponent and pass adirectionprop to it with a value ofvertical. This gives us a vertical layout.Line 9: We use the
Paragraphcomponent to show a new paragraph with the text ofMy Date Picker.Line 10: We use the
Datepickercomponent fromantd.Line 11: We make use of the
Buttoncomponent fromantd, and we use thetypeprop with the value ofprimary, which makes theButtona main/primary button.
Note: If we want to make use of
antdstyles in our application, we must also import theantdCSS file in our main application file. We can do this by importingantd/dist/reset.css, as seen in line 3 of theindex.jscode.
Conclusion
The Ant Design library offers various customizable UI components to enrich web applications. To learn more about the various available components, check their component overview.