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.9 KiB
5.9 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 "Use in create-react-app", 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">'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 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 "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>Now add the following jsx inside 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>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-test-with-jest">Test with Jest</h2> <p>If you use <code>create-react-app</code> follow the instructions <a href="/docs/react/use-with-create-react-app#Test-with-Jest">here</a> instead.</p> <p>Jest does not support <code>esm</code> modules, and Ant Design uses them. In order to test your Ant Design application with Jest you have to add the following to your Jest config :</p> <pre><code class="chroma language-json"><span class="s2">"transform"</span><span class="err">:</span> <span class="p">{</span> <span class="nt">"^.+\\.(ts|tsx|js|jsx)?$"</span><span class="p">:</span> <span class="s2">"ts-jest"</span> <span class="p">}</span> </code></pre><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>