logoAnt Design

⌘ K
  • Design
  • Development
  • Components
  • Blog
  • Resources
5.25.4
  • Ant Design of React
  • Changelog
    v5.25.4
  • Basic Usage
    • Getting Started
    • Usage with Vite
    • Usage with Next.js
      Updated
    • Usage with Umi
    • Usage with Rsbuild
    • Usage with Farm
    • Usage with Refine
  • Advanced
    • Customize Theme
    • CSS Compatible
    • Server Side Rendering
    • CSS Variables
      New
    • Use custom date library
    • Internationalization
    • Common Props
    • React 19 Compatibility
      New
  • Migration
    • V4 to V5
    • Less variables to Component Token
  • Other
    • Third-Party Libraries
    • Contributing
    • FAQ
Install and Initialization
Import antd
Using App Router
Using Pages Router

Usage with Next.js

contributors
  • Usage with ViteUsage with Umi

    Resources

    Ant Design X
    Ant Design Charts
    Ant Design Pro
    Pro Components
    Ant Design Mobile
    Ant Design Mini
    Ant Design Web3
    Ant Design Landing-Landing Templates
    Scaffolds-Scaffold Market
    Umi-React Application Framework
    dumi-Component doc generator
    qiankun-Micro-Frontends Framework
    Ant Motion-Motion Solution
    China Mirror 🇨🇳

    Community

    Awesome Ant Design
    Medium
    Twitter
    yuque logoAnt Design in YuQue
    Ant Design in Zhihu
    Experience Cloud Blog
    seeconf logoSEE Conf-Experience Tech Conference

    Help

    GitHub
    Change Log
    FAQ
    Bug Report
    Issues
    Discussions
    StackOverflow
    SegmentFault

    Ant XTech logoMore Products

    yuque logoYuQue-Document Collaboration Platform
    AntV logoAntV-Data Visualization
    Egg logoEgg-Enterprise Node.js Framework
    Kitchen logoKitchen-Sketch Toolkit
    Galacean logoGalacean-Interactive Graphics Solution
    xtech logoAnt Financial Experience Tech
    Theme Editor
    Made with ❤ by
    Ant Group and Ant Design Community

    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.

    Install and Initialization

    Before all start, you may need install yarn or pnpm or bun.

    npm iconnpm
    yarn iconyarn
    pnpm iconpnpm
    Bun LogoBun
    bash
    $ 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.

    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 or bun.

    npm iconnpm
    yarn iconyarn
    pnpm iconpnpm
    Bun LogoBun
    bash
    $ npm install antd --save

    Modify src/app/page.tsx, import Button component from antd.

    tsx
    import React from 'react';
    import { Button } from 'antd';
    const Home = () => (
    <div className="App">
    <Button type="primary">Button</Button>
    </div>
    );
    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.

    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.

    Using App Router Updated

    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/nextjs-registry
    npm iconnpm
    yarn iconyarn
    pnpm iconpnpm
    Bun LogoBun
    bash
    $ npm install @ant-design/nextjs-registry --save
    1. Use it in app/layout.tsx
    tsx
    import React from 'react';
    import { AntdRegistry } from '@ant-design/nextjs-registry';
    const RootLayout = ({ children }: React.PropsWithChildren) => (
    <html lang="en">
    <body>
    <AntdRegistry>{children}</AntdRegistry>
    </body>
    </html>
    );
    export default RootLayout;
    WARNING

    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.

    Using Pages Router

    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

    Notes for developers

    Please note that when you install @ant-design/cssinjs, you must ensure that the version is consistent with the version of @ant-design/cssinjs in local node_modules of antd, otherwise, multiple React instances will appear, resulting in ctx being unable to be read correctly. (Tips: you can use npm ls @ant-design/cssinjs command to view the local version)

    image
    npm iconnpm
    yarn iconyarn
    pnpm iconpnpm
    Bun LogoBun
    bash
    $ npm install @ant-design/cssinjs --save
    1. Rewrite pages/_document.tsx
    tsx
    import React from 'react';
    import { createCache, extractStyle, StyleProvider } from '@ant-design/cssinjs';
    import Document, { Head, Html, Main, NextScript } from 'next/document';
    import type { DocumentContext } from 'next/document';
    const MyDocument = () => (
    <Html lang="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) => (
    <StyleProvider cache={cache}>
    <App {...props} />
    </StyleProvider>
    ),
    });
    const initialProps = await Document.getInitialProps(ctx);
    const style = extractStyle(cache, true);
    return {
    ...initialProps,
    styles: (
    <>
    {initialProps.styles}
    <style dangerouslySetInnerHTML={{ __html: style }} />
    </>
    ),
    };
    };
    export default MyDocument;
    1. Supports custom themes
    ts
    // theme/themeConfig.ts
    import type { ThemeConfig } from 'antd';
    const theme: ThemeConfig = {
    token: {
    fontSize: 16,
    colorPrimary: '#52c41a',
    },
    };
    export default theme;
    1. Rewrite pages/_app.tsx
    tsx
    import React from 'react';
    import { ConfigProvider } from 'antd';
    import type { AppProps } from 'next/app';
    import theme from './theme/themeConfig';
    const App = ({ Component, pageProps }: AppProps) => (
    <ConfigProvider theme={theme}>
    <Component {...pageProps} />
    </ConfigProvider>
    );
    export default App;
    1. Use antd in page component
    tsx
    import React from 'react';
    import { Button } from 'antd';
    const Home = () => (
    <div className="App">
    <Button type="primary">Button</Button>
    </div>
    );
    export default Home;

    For more detailed information, please refer to with-nextjs-inline-style.