4.1 KiB
order | title |
---|---|
4 | Usage with Next.js |
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.
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.
Modify src/app/page.tsx
, import Button component from antd
.
'use client';
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!
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.
- Install
@ant-design/cssinjs
- Create
lib/AntdRegistry.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(() => (
<style id="antd" dangerouslySetInnerHTML={{ __html: extractStyle(cache, true) }} />
));
return <StyleProvider cache={cache}>{children}</StyleProvider>;
};
export default StyledComponentsRegistry;
- Use it in
app/layout.tsx
import { Inter } from 'next/font/google';
import React from 'react';
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;
- 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;
- Use in page
'use client';
import { Button, ConfigProvider } from 'antd';
import React from 'react';
import theme from './themeConfig';
const HomePage: React.FC = () => (
<ConfigProvider theme={theme}>
<div className="App">
<Button type="primary">Button</Button>
</div>
</ConfigProvider>
);
export default HomePage;
For more detailed information, please refer to with-nextjs-app-router-inline-style。