I've been trying to figure out how I can use Ant Design (antd) in my next.js application. And it seems like I just can't do that. More precisely, I can, but then I just have to render everything on the client side, because almost every Ant Design component uses things that are not allowed for server-side components. Even the most simple things like and .
-
This question was closed falsely in my opinion. You'll find the answer to your question [here](https://mobile.ant.design/guide/ssr/#server-side-rendering--ssr). – Fabio Nettis Jul 17 '23 at 07:15
-
@FabioNettis thanks a lot. I checked it. And it says I have to add 'use client' at the top of my files. It means I just can't avoid CSR, doesn't it? )) – Oleg Ivsv Jul 17 '23 at 07:32
-
1Since client components can still receive server components as children, you're not completely dependent on client side rendering, ant-d seems to use context in it's components, henceforth the need for the `use-client` directive. – Fabio Nettis Jul 17 '23 at 07:38
1 Answers
For anybody else stumbling upon this issue, based on the official documentation of Ant Design, you need to follow a few steps, based on the version of Next.js you are using.
Next.js 12
Using antd-mobile in Next.js requires some additional configuration. First, you need to install the next-transpile-modules
package, then modify your next.config.js
.
const withTM = require('next-transpile-modules')([
'antd-mobile',
]);
module.exports = withTM({
// other Next.js configuration in your project
});
Next.js 13
Next.js 13 can automatically transpile and bundle dependencies from external dependencies (node_modules). This replaces the next-transpile-modules
package. Simply add this code to your next.config.js
.
module.exports = {
transpilePackages: ['antd-mobile'],
// other Next.js configuration in your project
};
When using Next.js 13 you are required to add the 'use client' directive at the top of the file when using ant-d components inside it. Since client components can still receive server components as children, you're not completely dependent on client side rendering.

- 1,150
- 6
- 18