- Ant Design of React
- Changelogv6.1.4
- Basic Usage
- AI
- Advanced
- Migration
- Other
Next.js is currently the most popular React server-side isomorphic framework in the world. This article will try to use antd components in projects created by Next.js.
$ npx create-next-app antd-demo
The tool will create and initialize environment and dependencies automatically, please try config your proxy setting, or use another npm registry if any network errors happen during it.
After the initialization is complete, we enter the project and start.
$ cd antd-demo$ npm run dev
Open the browser at http://localhost:3000/. if you see the NEXT logo, it is considered a success.
Now we install antd from yarn or npm or pnpm or bun.
$ npminstall antd --save
Modify src/app/page.tsx, import Button component from antd.
importReactfrom'react';import{Button}from'antd';constHome=()=>(<divclassName="App"><Buttontype="primary">Button</Button></div>);exportdefaultHome;
OK, you should now see a blue primary button displayed on the page. Next you can choose any components of antd to develop your application. Visit other workflows of Next.js at its User Guide.
You could find that components of antd do not have styles in the first screen. Next, you need to choose different SSR style processing methods according to the mode of Next.js.
If you are using the App Router in Next.js and using antd as your component library, to make the antd component library work better in your Next.js application and provide a better user experience, you can try using the following method to extract and inject antd's first-screen styles into HTML to avoid page flicker.
@ant-design/nextjs-registry$ npminstall @ant-design/nextjs-registry --save
app/layout.tsximportReactfrom'react';import{AntdRegistry}from'@ant-design/nextjs-registry';constRootLayout=({ children }:React.PropsWithChildren)=>(<htmllang="en"><body><AntdRegistry>{children}</AntdRegistry></body></html>);exportdefaultRootLayout;
Next.js App Router currently not support using sub-components via . like <Select.Option /> and <Typography.Text />. Importing them from path would solve this problem.
For more detailed information, please refer to with-nextjs-app-router-inline-style.
If you are using the Pages Router in Next.js and using antd as your component library, to make the antd component library work better in your Next.js application and provide a better user experience, you can try using the following method to extract and inject antd's first-screen styles into HTML to avoid page flicker.
@ant-design/cssinjs@1.xNotes for developers
Please note that when you install
image@ant-design/cssinjs, you must ensure that the version is consistent with the version of@ant-design/cssinjsin localnode_modulesofantd, otherwise, multiple React instances will appear, resulting in ctx being unable to be read correctly. (Tips: you can usenpm ls @ant-design/cssinjscommand to view the local version)
$ npminstall @ant-design/cssinjs --save
pages/_document.tsximportReactfrom'react';import{ createCache, extractStyle,StyleProvider}from'@ant-design/cssinjs';importDocument,{Head,Html,Main,NextScript}from'next/document';importtype{DocumentContext}from'next/document';constMyDocument=()=>(<Htmllang="en"><Head/><body><Main/><NextScript/></body></Html>);MyDocument.getInitialProps=async(ctx:DocumentContext)=>{const cache =createCache();const originalRenderPage = ctx.renderPage;ctx.renderPage=()=>originalRenderPage({enhanceApp:(App)=>(props)=>(<StyleProvidercache={cache}><App{...props}/></StyleProvider>),});const initialProps =awaitDocument.getInitialProps(ctx);const style =extractStyle(cache,true);return{...initialProps,styles:(<>{initialProps.styles}<styledangerouslySetInnerHTML={{ __html: style }}/></>),};};exportdefaultMyDocument;
// theme/themeConfig.tsimporttype{ ThemeConfig }from'antd';const theme: ThemeConfig ={token:{fontSize:16,colorPrimary:'#52c41a',},};exportdefault theme;
pages/_app.tsximportReactfrom'react';import{ConfigProvider}from'antd';importtype{AppProps}from'next/app';importthemefrom'./theme/themeConfig';constApp=({Component, pageProps }:AppProps)=>(<ConfigProvidertheme={theme}><Component{...pageProps}/></ConfigProvider>);exportdefaultApp;
importReactfrom'react';import{Button}from'antd';constHome=()=>(<divclassName="App"><Buttontype="primary">Button</Button></div>);exportdefaultHome;
For more detailed information, please refer to with-nextjs-inline-style.