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.
 
 

5.5 KiB

order title
2 Getting Started

Ant Design React is dedicated to providing a good development experience for programmers. Before starting, it is recommended to learn React and ES2015 first, and correctly install and configure Node.js v8 or above.

The official guide also assumes that you have intermediate knowledge about HTML, CSS, and JavaScript, and React. If you are just starting to learn front-end or React, it may not be the best idea to use the UI framework as your first step.

Finally, if you are working in a local development environment, please refer to Install and Initialization section of "Use in create-react-app".


Your First Example

Here is a simple online codesandbox demo of an Ant Design component to show the usage of Ant Design React.

<p>Follow the steps below to play around with Ant Design yourself:</p> <h3 id="user-content-1-create-a-codesandbox">1. Create a codesandbox</h3> <p>Visit http://u.ant.design/codesandbox-repro to create a codesandbox -- don't forget to press the save button as well to create a new instance.</p> <h3 id="user-content-2-use-and-modify-an-antd-component">2. Use and modify an antd component</h3> <p>Replace the contents 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 have already set things up by following the <a href="/docs/react/use-with-create-react-app#Install-and-Initialization">Install and Initialization</a> section of &quot;Use in create-react-app&quot;, replace the content of <code>/src/index.js</code> as follows:</p> <pre><code class="chroma language-jsx"><span class="kr">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">useState</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;react&#39;</span><span class="p">;</span> <span class="kr">import</span> <span class="p">{</span> <span class="nx">render</span> <span class="p">}</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">const</span> <span class="nx">App</span> <span class="o">=</span> <span class="p">()</span> <span class="p">=&gt;</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="nx">setDate</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">(</span><span class="kc">null</span><span class="p">);</span> <span class="kr">const</span> <span class="nx">handleChange</span> <span class="o">=</span> <span class="nx">value</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">value</span> <span class="o">?</span> <span class="nx">value</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="nx">setDate</span><span class="p">(</span><span class="nx">value</span><span class="p">);</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="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">16</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="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 view the list of components in the side menu of the Components page, such as the <a href="/components/alert">Alert</a> component. Plenty of examples are also provided in the component pages and API documentation as well.</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>Now add the following jsx inside 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=&#34;Selected Date&#34; description={date ? date.format(&#39;YYYY-MM-DD&#39;) : &#39;None&#39;} /&gt; </span><span class="gi"></span> &lt;/div&gt; </code></pre><p>Select a date, and you can see the effect in the preview area on the right:</p> <p><img width="420" src="https://gw.alipayobjects.com/zos/antfincdn/JrXptUm1Nz/6b50edc4-3a3c-4b2a-843e-f9f0af2c4667.png" alt="codesandbox screenshot" /></p> <p>OK! Now that you know the basics of using antd components, you are welcome to explore more components in the codesandbox. When reporting a bug with ant design, we also strongly recommend using codesandbox to provide a reproducible demo as well.</p> <h3 id="user-content-4-next-steps">4. Next Steps</h3> <p>During actual real-world project development, you will most likely need a development workflow consisting of <code>compile/build/deploy/lint/debug/</code> deployment. You can read the following documents on the subject or use the following scaffolds and examples 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-import-on-demand">Import on Demand</h2> <p><code>antd</code> supports tree shaking of ES modules, so using <code>import { Button } from 'antd';</code> would drop js code you didn't use.</p> <p>If you see logs like in the screenshot below, you might still be using <code>webpack@1.x</code> or have a wrong webpack config which can't support tree shaking.</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. Please upgrade webpack or check the config. </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> <h2 id="user-content-customize-your-workflow">Customize your Workflow</h2> <p>If you want to customize your workflow, we recommend using <a href="http://webpack.github.io/">webpack</a> to build and debug code. You can try out plenty of <a href="https://github.com/enaqx/awesome-react#react-tools">boilerplates</a> available in the React ecosystem.</p> <p>There are also some <a href="http://scaffold.ant.design/">scaffolds</a> which have already been integrated into antd, so you can try and start with one of these and even contribute.</p>