logoNamu Design

⌘ K
  • Design
  • Development
  • Components
  • Blog
  • Resources
1.0.0
  • Namu Design of React
  • Changelogv1.0.0
  • Basic Usage
    • Getting Started
    • Usage with create-react-app
    • Usage with Next.js
    • Usage with Umi
    • Usage with Vite
  • Advanced
    • Customize ThemeUpdated
    • CSS Compatible
    • Server Side RenderingNew
    • Use custom date library
    • Internationalization
  • Migration
    • V4 to V5
    • Less variables to Component Token
  • Other
    • Common Props
    • Third-Party Libraries
    • Contributing
    • FAQ
Install and Initialization
Import antd
Use the Pages Router of Next.js
Using Next.js App Router

Usage with Next.js

  • Usage with create-react-appUsage with Umi

    Resources

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

    Community

    Awesome Namu Design
    Medium
    Twitter
    yuqueNamu Design in YuQue
    Namu Design in Zhihu
    Experience Cloud Blog
    seeconfSEE Conf-Experience Tech Conference

    Help

    GitHub
    Change Log
    FAQ
    Bug Report
    Issues
    Discussions
    StackOverflow
    SegmentFault

    Ant XTechMore Products

    yuqueYuQue-Document Collaboration Platform
    AntVAntV-Data Visualization
    EggEgg-Enterprise Node.js Framework
    kitchenKitchen-Sketch Toolkit
    xtechAnt Financial Experience Tech
    Theme Editor
    Made with ❤ by
    Ant Group and Namu 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.

    npm
    yarn
    pnpm
    $ 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.

    Import antd

    Now we install antd from yarn or npm or pnpm.

    npm
    yarn
    pnpm
    $ npm install antd --save

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

    'use client'; // If used in Pages Router, is no need to add this line
    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.

    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
    npm
    yarn
    pnpm
    $ npm install @ant-design/cssinjs --save
    1. Rewrite pages/_document.tsx
    import React from 'react';
    import { StyleProvider, createCache, extractStyle } 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
    // theme/themeConfig.ts
    import type { ThemeConfig } from 'antd';
    const theme: ThemeConfig = {
    token: {
    fontSize: 16,
    colorPrimary: '#52c41a',
    },
    };
    export default theme;
    1. Rewrite pages/_app.tsx
    import React from 'react';
    import { ConfigProvider } from 'antd';
    import type { AppProps } from 'next/app';
    import theme from './themeConfig';
    const App = ({ Component, pageProps }: AppProps) => (
    <ConfigProvider theme={theme}>
    <Component {...pageProps} />
    </ConfigProvider>
    );
    export default App;
    1. Use antd in page component
    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.

    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
    npm
    yarn
    pnpm
    $ npm install @ant-design/cssinjs --save
    1. Create lib/AntdRegistry.tsx
    'use client';
    import React from 'react';
    import { StyleProvider, createCache, extractStyle } from '@ant-design/cssinjs';
    import { useServerInsertedHTML } from 'next/navigation';
    const StyledComponentsRegistry = ({ children }: { children: React.ReactNode }) => {
    const cache = createCache();
    useServerInsertedHTML(() => (
    <style id="antd" dangerouslySetInnerHTML={{ __html: extractStyle(cache, true) }} />
    ));
    return <StyleProvider cache={cache}>{children}</StyleProvider>;
    };
    export default StyledComponentsRegistry;
    1. Use it in app/layout.tsx
    import React from 'react';
    import { Inter } from 'next/font/google';
    import StyledComponentsRegistry from '../lib/AntdRegistry';
    import '@/globals.css';
    const inter = Inter({ subsets: ['latin'] });
    export const metadata = {
    title: 'Create Next App',
    description: 'Generated by create next app',
    };
    const RootLayout = ({ children }: { children: React.ReactNode }) => (
    <html lang="en">
    <body className={inter.className}>
    <StyledComponentsRegistry>{children}</StyledComponentsRegistry>
    </body>
    </html>
    );
    export default RootLayout;
    1. Customize theme config in theme/*.ts
    // theme/themeConfig.ts
    import type { ThemeConfig } from 'antd';
    const theme: ThemeConfig = {
    token: {
    fontSize: 16,
    colorPrimary: '#52c41a',
    },
    };
    export default theme;
    1. Use in page
    import React from 'react';
    import { Button, ConfigProvider } from 'antd';
    import theme from './themeConfig';
    const HomePage = () => (
    <ConfigProvider theme={theme}>
    <div className="App">
    <Button type="primary">Button</Button>
    </div>
    </ConfigProvider>
    );
    export default HomePage;

    Tips: The above method does not use sub-components such as <Select.Option /> and <Typography.text /> in the page, so it can be used normally. However, if you use a sub-component like this in your page, you will currently see the following warning in next.js: Error: Cannot access .Option on the server. You cannot dot into a client module from a server component. You can only pass the imported name through., currently need to wait for Next.js official solution. Before again, if you use the above sub-components in your page, you can add "use client" to the first line of the page component to avoid warnings. See examples for more details: with-sub-components.

    For more detailed information, please refer to with-nextjs-app-router-inline-style。