logoAnt Design X

DesignDevelopmentComponentsPlayground
  • Ant Design X of React
  • Changelog
    v1.2.0
  • Basic Usage
    • Usage with create-react-app
    • Usage with Vite
    • Usage with Next.js
    • Usage with Umi
    • Usage with Rsbuild
  • Model Integration
    • OpenAI
      Updated
    • Qwen
      Updated
    • Others
  • Other
    • Contributing
    • dangerouslyApiKey Explanation
    • FAQ

Usage with Next.js

Resources

Ant Design
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
loading

Here’s the translation of your guide on using @ant-design/x with Next.js:


Next.js is currently one of the most popular React server-side rendering frameworks. This article will guide you on how to use @ant-design/x components in a Next.js project.

Installation and Initialization

Before you start, you might need to install yarn, pnpm, or bun.

npm iconnpm
yarn iconyarn
pnpm iconpnpm
Bun LogoBun
bash
$ npx create-next-app antdx-demo

The tool will automatically initialize a scaffold and install various necessary dependencies. During the installation process, you may need to choose some configuration options. If you encounter network issues, try configuring a proxy or using another npm registry, such as switching to the Taobao mirror: npm config set registry https://registry.npmmirror.com.

Once the initialization is complete, navigate to the project directory and start the development server.

bash
$ cd antdx-demo
$ npm run dev

Visit http://localhost:3000/ in your browser, and seeing the Next.js logo means the setup is successful.

Importing @ant-design/x

Now, install and import @ant-design/x from yarn, npm, pnpm, or bun.

npm iconnpm
yarn iconyarn
pnpm iconpnpm
Bun LogoBun
bash
$ npm install @ant-design/x --save

Modify src/app/page.tsx to import the Bubble component from @ant-design/x.

tsx
import React from 'react';
import { Bubble } from '@ant-design/x';
const Home = () => (
<div className="App">
<Bubble content="Hello world!" />
</div>
);
export default Home;

You should now see the Bubble component from @ant-design/x on your page. You can proceed to use other components to develop your application. For other development processes, you can refer to the official Next.js documentation.

You may notice that the @ant-design/x component does not have styles on the first screen. Below, we'll address how to handle SSR (Server-Side Rendering) styles for Next.js.

Using App Router Updated

If you are using the App Router in Next.js and @ant-design/x as your component library, you can improve user experience by extracting and injecting @ant-design/x's initial styles into HTML to avoid page flashes.

  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

Note: The Next.js App Router does not currently support importing child components directly using . (e.g., <Select.Option />, <Typography.Text />). To avoid errors, import these child components from their respective paths.

For more details, refer to with-nextjs-app-router-inline-style.

Using Pages Router

If you are using the Pages Router in Next.js and @ant-design/x as your component library, you can improve user experience by extracting and injecting @ant-design/x's initial styles into HTML to avoid page flashes.

  1. Install @ant-design/cssinjs

Developer Note:

Ensure that the version of @ant-design/cssinjs installed matches the version in @ant-design/x's local node_modules, to avoid issues with multiple React instances. (Tip: You can check the local version with npm ls @ant-design/cssinjs.)

npm iconnpm
yarn iconyarn
pnpm iconpnpm
Bun LogoBun
bash
$ npm install @ant-design/cssinjs --save
  1. Modify 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. Support for Custom Themes
ts
// theme/themeConfig.ts
import type { ThemeConfig } from 'antd';
const theme: ThemeConfig = {
token: {
fontSize: 16,
colorPrimary: '#52c41a',
},
};
export default theme;
  1. Modify pages/_app.tsx
tsx
import React from 'react';
import { XProvider } from '@ant-design/x';
import type { AppProps } from 'next/app';
import theme from './theme/themeConfig';
const App = ({ Component, pageProps }: AppProps) => (
<XProvider theme={theme}>
<Component {...pageProps} />
</XProvider>
);
export default App;
  1. Use @ant-design/x in your pages
tsx
import React from 'react';
import { Bubble } from '@ant-design/x';
const Home = () => (
<div className="App">
<Bubble content="Hello world!" />
</div>
);
export default Home;

For more details, refer to with-nextjs-inline-style.