--- order: 4 title: Usage with Next.js --- [Next.js](https://nextjs.org/) 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. ## Install and Initialization Before all start, you may need install [yarn](https://github.com/yarnpkg/yarn/) or [pnpm](https://pnpm.io/). 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. ```bash $ cd antd-demo $ npm run dev ``` Open the browser at http://localhost:3000/. if you see the NEXT logo, it is considered a success. ## Import antd Now we install `antd` from yarn or npm or pnpm. Modify `src/app/page.tsx`, import Button component from `antd`. ```jsx import React from 'react'; import { Button } from 'antd'; const Home = () => (
); export default Home; ``` 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](https://nextjs.org/). We are successfully running antd components now, go build your own application! ## Use the Pages Router of Next.js 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. 1. Install `@ant-design/cssinjs` 2. Rewrite `pages/_document.tsx` ```tsx import { StyleProvider, createCache, extractStyle } from '@ant-design/cssinjs'; import Document, { DocumentContext, Head, Html, Main, NextScript } from 'next/document'; const MyDocument = () => (
); MyDocument.getInitialProps = async (ctx: DocumentContext) => { const cache = createCache(); const originalRenderPage = ctx.renderPage; ctx.renderPage = () => originalRenderPage({ enhanceApp: (App) => (props) => ( ), }); const initialProps = await Document.getInitialProps(ctx); // 1.1 extract style which had been used const style = extractStyle(cache, true); return { ...initialProps, styles: ( <> {initialProps.styles} {/* 1.2 inject css */} ), }; }; export default MyDocument; ``` 3. Supports custom themes ```ts // theme/themeConfig.ts import type { ThemeConfig } from 'antd'; const theme: ThemeConfig = { token: { fontSize: 16, colorPrimary: '#52c41a', }, }; export default theme; ``` 4. Rewrite `pages/_app.tsx` ```tsx import { ConfigProvider } from 'antd'; import type { AppProps } from 'next/app'; import theme from './themeConfig'; const App = ({ Component, pageProps }: AppProps) => ( ); export default App; ``` 5. Use antd in page component ```tsx import { Button } from 'antd'; const Home = () => (
); export default Home; ``` For more detailed information, please refer to [with-nextjs-inline-style](https://github.com/ant-design/ant-design-examples/tree/main/examples/with-nextjs-inline-style). ## Using Next.js App Router 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. 1. Install `@ant-design/cssinjs` 2. Create `lib/AntdRegistry.tsx` ```tsx 'use client'; import { StyleProvider, createCache, extractStyle } from '@ant-design/cssinjs'; import { useServerInsertedHTML } from 'next/navigation'; import React from 'react'; const StyledComponentsRegistry = ({ children }: { children: React.ReactNode }) => { const cache = createCache(); useServerInsertedHTML(() => (