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.3 KiB
5.3 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="/docs/react/use-with-create-react-app#Install-and-Initialization">Install and Initialization</a> section of "Use in create-react-app", 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="p">,</span> <span class="p">{</span> <span class="nx">useState</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">'react'</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">'react-dom'</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">'antd'</span><span class="p">;</span> <span class="kr">import</span> <span class="s1">'antd/dist/antd.css'</span><span class="p">;</span> <span class="kr">import</span> <span class="s1">'./index.css'</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">=></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">=></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">'YYYY-MM-DD'</span><span class="p">)</span> <span class="o">:</span> <span class="s1">'None'</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"><</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">'100px auto'</span> <span class="p">}}></span> <span class="p"><</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">/></span> <span class="p"><</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">}}></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">'YYYY-MM-DD'</span><span class="p">)</span> <span class="o">:</span> <span class="s1">'None'</span><span class="p">}</span> <span class="p"></</span><span class="nt">div</span><span class="p">></span> <span class="p"></</span><span class="nt">div</span><span class="p">></span> <span class="p">);</span> <span class="p">};</span> <span class="nx">render</span><span class="p">(<</span><span class="nt">App</span> <span class="p">/>,</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s1">'root'</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 "Open in Editor" 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 'antd'; </span><span class="gd"></span><span class="gi">+ import { DatePicker, message, Alert } from 'antd'; </span></code></pre><p>Add the following jsx into the <code>render</code> function.</p> <pre><code class="chroma language-diff"> <DatePicker onChange={value => this.handleChange(value)} /> <div style={{ marginTop: 20 }}> <span class="gd">- Selected Date: {date ? date.format('YYYY-MM-DD') : 'None'} </span><span class="gd"></span><span class="gi">+ <Alert message="Selected Date" description={date ? date.format('YYYY-MM-DD') : 'None'} /> </span><span class="gi"></span> </div> </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/JrXptUm1Nz/6b50edc4-3a3c-4b2a-843e-f9f0af2c4667.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-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-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">'antd-dayjs-webpack-plugin'</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-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. You can try out plents of <a href="https://github.com/enaqx/awesome-react#react-tools">boilerplates</a> available in the React ecosystem.</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>