You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

8.5 KiB

order title
2 Getting Started

Ant Design React is dedicated to providing a good development experience for programmers. Make sure that you have installed Node.js(> 8.0.0) correctly.

If you try in local environment, Please refer to Install and Initialization section of "Use in create-react-app".

Before delving into Ant Design React, a good knowledge base of React and JavaScript ES2015 is needed.


First Example

Here is a simple codesandbox example to show the usage of Ant Design React.

<h3 id="user-content-1-create-one-codesandbox">1. Create one codesandbox</h3> <p>Visit http://u.ant.design/codesandbox-repro to create a codesandbox. Don't forget to press the save button.</p> <h3 id="user-content-2-using-antd-component">2. Using antd component</h3> <p>Replace the content of <code>index.js</code> with the following code. As you can see, there is no difference between antd's components and typical React components.</p> <p>If you already set up by <a href="https://ant.design/docs/react/use-with-create-react-app#Install-and-Initialization">Install and Initialization</a> section of &quot;Use in create-react-app&quot;, Please replace the content of /src/index.js</p> <pre><code class="chroma language-jsx"><span class="kr">import</span> <span class="nx">React</span> <span class="nx">from</span> <span class="s1">&#39;react&#39;</span><span class="p">;</span> <span class="kr">import</span> <span class="nx">ReactDOM</span> <span class="nx">from</span> <span class="s1">&#39;react-dom&#39;</span><span class="p">;</span> <span class="kr">import</span> <span class="p">{</span> <span class="nx">DatePicker</span><span class="p">,</span> <span class="nx">message</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;antd&#39;</span><span class="p">;</span> <span class="kr">import</span> <span class="s1">&#39;antd/dist/antd.css&#39;</span><span class="p">;</span> <span class="kr">import</span> <span class="s1">&#39;./index.css&#39;</span><span class="p">;</span> <span class="kr">class</span> <span class="nx">App</span> <span class="kr">extends</span> <span class="nx">React</span><span class="p">.</span><span class="nx">Component</span> <span class="p">{</span> <span class="nx">state</span> <span class="o">=</span> <span class="p">{</span> <span class="nx">date</span><span class="o">:</span> <span class="kc">null</span><span class="p">,</span> <span class="p">};</span> <span class="nx">handleChange</span> <span class="o">=</span> <span class="nx">date</span> <span class="p">=&gt;</span> <span class="p">{</span> <span class="nx">message</span><span class="p">.</span><span class="nx">info</span><span class="p">(</span><span class="sb">`Selected Date: </span><span class="si">${</span><span class="nx">date</span> <span class="o">?</span> <span class="nx">date</span><span class="p">.</span><span class="nx">format</span><span class="p">(</span><span class="s1">&#39;YYYY-MM-DD&#39;</span><span class="p">)</span> <span class="o">:</span> <span class="s1">&#39;None&#39;</span><span class="si">}</span><span class="sb">`</span><span class="p">);</span> <span class="k">this</span><span class="p">.</span><span class="nx">setState</span><span class="p">({</span> <span class="nx">date</span> <span class="p">});</span> <span class="p">};</span> <span class="nx">render</span><span class="p">()</span> <span class="p">{</span> <span class="kr">const</span> <span class="p">{</span> <span class="nx">date</span> <span class="p">}</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">state</span><span class="p">;</span> <span class="k">return</span> <span class="p">(</span> <span class="p">&lt;</span><span class="nt">div</span> <span class="na">style</span><span class="o">=</span><span class="p">{{</span> <span class="nx">width</span><span class="o">:</span> <span class="mi">400</span><span class="p">,</span> <span class="nx">margin</span><span class="o">:</span> <span class="s1">&#39;100px auto&#39;</span> <span class="p">}}&gt;</span> <span class="p">&lt;</span><span class="nt">DatePicker</span> <span class="na">onChange</span><span class="o">=</span><span class="p">{</span><span class="k">this</span><span class="p">.</span><span class="nx">handleChange</span><span class="p">}</span> <span class="p">/&gt;</span> <span class="p">&lt;</span><span class="nt">div</span> <span class="na">style</span><span class="o">=</span><span class="p">{{</span> <span class="nx">marginTop</span><span class="o">:</span> <span class="mi">20</span> <span class="p">}}&gt;</span> <span class="nx">Selected</span> <span class="nb">Date</span><span class="o">:</span> <span class="p">{</span><span class="nx">date</span> <span class="o">?</span> <span class="nx">date</span><span class="p">.</span><span class="nx">format</span><span class="p">(</span><span class="s1">&#39;YYYY-MM-DD&#39;</span><span class="p">)</span> <span class="o">:</span> <span class="s1">&#39;None&#39;</span><span class="p">}</span> <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span> <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span> <span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="nx">ReactDOM</span><span class="p">.</span><span class="nx">render</span><span class="p">(&lt;</span><span class="nt">App</span> <span class="p">/&gt;,</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s1">&#39;root&#39;</span><span class="p">));</span> </code></pre><h3 id="user-content-3-explore-more-components">3. Explore more components</h3> <p>You can look up components in the side menu of the Components page like the <a href="/components/alert">Alert</a> component. Plenty of examples are provided in the component pages and API documentation.</p> <p>Click the &quot;Open in Editor&quot; icon in the first example to open an editor with source code to use out-of-the-box. Now you can import the <code>Alert</code> component into the codesandbox:</p> <pre><code class="chroma language-diff"><span class="gd">- import { DatePicker, message } from &#39;antd&#39;; </span><span class="gd"></span><span class="gi">+ import { DatePicker, message, Alert } from &#39;antd&#39;; </span></code></pre><p>Add the following jsx into the <code>render</code> function.</p> <pre><code class="chroma language-diff"> &lt;DatePicker onChange={value =&gt; this.handleChange(value)} /&gt; &lt;div style={{ marginTop: 20 }}&gt; <span class="gd">- Selected Date: {date ? date.format(&#39;YYYY-MM-DD&#39;) : &#39;None&#39;} </span><span class="gd"></span><span class="gi">+ &lt;Alert message={`Selected Date: ${date ? date.format(&#39;YYYY-MM-DD&#39;) : &#39;None&#39;}`} type=&#34;success&#34; /&gt; </span><span class="gi"></span> &lt;/div&gt; </code></pre><p>Now you can see the result in the preview section.</p> <p><img width="420" src="https://gw.alipayobjects.com/zos/antfincdn/QjCr7oLcpT/c7ce72d2-601e-4130-a33b-456d4652bb2d.png" alt="codesandbox screenshot" /></p> <p>OK! Now you know how to use antd components in a clear way. You are welcome to explore more components in the codesandbox. We also strongly recommend using codesandbox to provide a reproducible demo when reporting a bug.</p> <h3 id="user-content-4-next-step">4. Next Step</h3> <p>In the real world you will need a development workflow consisting of <code>compile/build/deploy/lint/debug</code>. You can find and read articles on the subject or try other scaffolds provided below:</p> <ul> <li><a href="http://pro.ant.design/">Ant Design Pro</a></li> <li><a href="https://github.com/zuiidea/antd-admin">antd-admin</a></li> <li><a href="https://github.com/d2-projects/d2-admin">d2-admin</a></li> <li>more scaffolds at <a href="http://scaffold.ant.design/">Scaffold Market</a></li> </ul> <h2 id="user-content-compatibility">Compatibility</h2> <p>Ant Design React supports all modern browsers and IE11+.</p> <table> <thead> <tr> <th><a href="http://godban.github.io/browsers-support-badges/"><img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/edge/edge_48x48.png" alt="IE / Edge" width="24px" height="24px" /></a></br>IE / Edge</th> <th><a href="http://godban.github.io/browsers-support-badges/"><img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/firefox/firefox_48x48.png" alt="Firefox" width="24px" height="24px" /></a></br>Firefox</th> <th><a href="http://godban.github.io/browsers-support-badges/"><img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/chrome/chrome_48x48.png" alt="Chrome" width="24px" height="24px" /></a></br>Chrome</th> <th><a href="http://godban.github.io/browsers-support-badges/"><img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/safari/safari_48x48.png" alt="Safari" width="24px" height="24px" /></a></br>Safari</th> <th><a href="http://godban.github.io/browsers-support-badges/"><img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/opera/opera_48x48.png" alt="Opera" width="24px" height="24px" /></a></br>Opera</th> <th><a href="http://godban.github.io/browsers-support-badges/"><img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/electron/electron_48x48.png" alt="Electron" width="24px" height="24px" /></a></br>Electron</th> </tr> </thead> <tbody> <tr> <td>IE11, Edge</td> <td>last 2 versions</td> <td>last 2 versions</td> <td>last 2 versions</td> <td>last 2 versions</td> <td>last 2 versions</td> </tr> </tbody> </table> <p>Polyfills are needed for IE browsers. We recommend <a href="https://babeljs.io/docs/en/babel-preset-env">babel-preset-env</a> for it. You can set <code>targets</code> config if you are using <a href="http://umijs.org/">umi</a>.</p> <p>Ant Design 3.0 supports both React 15 and 16 but we strongly suggest React 16 for better performance and fewer bugs.</p> <h4 id="user-content-ie-note">IE note</h4> <blockquote> <p>We drop support of IE8 after <code>antd@2.0</code>, IE9/10 after <code>antd@4.0</code>,</p> </blockquote> <h2 id="user-content-customized-work-flow">Customized Work Flow</h2> <p>If you want to customize your work flow, we recommend using <a href="http://webpack.github.io/">webpack</a> to build and debug code.</p> <p>Also, you can use any <a href="https://github.com/enaqx/awesome-react#boilerplates">scaffold</a> available in the React ecosystem. If you encounter problems, you can use our <a href="https://github.com/ant-tool/atool-build/blob/master/src/getWebpackCommonConfig.js">webpack config</a> and <a href="http://ant-tool.github.io/webpack-config.html">modify it</a>.</p> <p>If you are trying <a href="https://parceljs.org">parcel</a>, here is <a href="https://github.com/ant-design/parcel-antd">a demo repository</a>.</p> <p>There are some <a href="http://scaffold.ant.design/">scaffolds</a> which have already integrated antd, so you can try and start with one of these and even contribute.</p> <h2 id="user-content-import-on-demand">Import on Demand</h2> <p>If you see logs like in the screenshot below, you might be importing all components by writing <code>import { Button } from 'antd';</code>. This will affect your app's network performance.</p> <pre><code class="chroma language-text">You are using a whole package of antd, please use https://www.npmjs.com/package/babel-plugin-import to reduce app bundle size. </code></pre><blockquote> <p><a href="https://zos.alipayobjects.com/rmsportal/GHIRszVcmjccgZRakJDQ.png"><img src="https://zos.alipayobjects.com/rmsportal/GHIRszVcmjccgZRakJDQ.png" alt="console warning"></a></p> </blockquote> <p>However, we can import individual components on demand:</p> <pre><code class="chroma language-jsx"><span class="kr">import</span> <span class="nx">Button</span> <span class="nx">from</span> <span class="s1">&#39;antd/es/button&#39;</span><span class="p">;</span> <span class="kr">import</span> <span class="s1">&#39;antd/es/button/style&#39;</span><span class="p">;</span> <span class="c1">// or antd/es/button/style/css for css format file </span></code></pre><blockquote> <p>Note: antd supports ES6 tree shaking, so <code>import { Button } from 'antd';</code> will drop the js code you don't use too.</p> </blockquote> <p>We strongly recommend using <a href="https://github.com/ant-design/babel-plugin-import">babel-plugin-import</a>, which can convert the following code to the 'antd/es/xxx' way:</p> <pre><code class="chroma language-jsx"><span class="kr">import</span> <span class="p">{</span> <span class="nx">Button</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;antd&#39;</span><span class="p">;</span> </code></pre><p>And this plugin can load styles too. Read <a href="https://github.com/ant-design/babel-plugin-import#usage">usage</a> for more details.</p> <blockquote> <p>FYI, babel-plugin-import's <code>style</code> option will importing some global reset styles, don't use it if you don't need those styles. You can import styles manually via <code>import 'antd/dist/antd.css'</code> and override the global reset styles.</p> </blockquote> <h2 id="user-content-replace-momentjs-to-day-js">Replace momentjs to Day.js</h2> <p>You can use <a href="https://github.com/ant-design/antd-dayjs-webpack-plugin">antd-dayjs-webpack-plugin</a> plugin to replace momentjs to Day.js to reduce bundle size dramatically. You need to update your webpack config file like this:</p> <pre><code class="chroma language-js"><span class="c1">// webpack-config.js </span><span class="c1"></span><span class="kr">import</span> <span class="nx">AntdDayjsWebpackPlugin</span> <span class="nx">from</span> <span class="s1">&#39;antd-dayjs-webpack-plugin&#39;</span><span class="p">;</span> <span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="p">{</span> <span class="c1">// ... </span><span class="c1"></span> <span class="nx">plugins</span><span class="o">:</span> <span class="p">[</span><span class="k">new</span> <span class="nx">AntdDayjsWebpackPlugin</span><span class="p">()],</span> <span class="p">};</span> </code></pre><h2 id="user-content-customization">Customization</h2> <ul> <li><a href="/docs/react/customize-theme">Customize Theme</a></li> <li><a href="https://github.com/ant-design/antd-init/tree/master/examples/local-iconfont">Local Iconfont</a></li> </ul>