Next.js & Ant Design - Custom ThemedLayoutV2 not working
Hi, I tried to swizzle the ThemedLayout as I need to edit the Sider. But if I follow the documentation:
src/app/layout.tsx
then I was getting errors that I can't use hooks in a server component. So I created client component refine-context.tsx:
and src/app/layout.tsx:
Then I end up with "duplicate" layout (the customized layout is correct, but then I am getting the default one inside the customized one..)
Does anyone know how to sort that out properly?
Thank you in advance!

4 Replies
Hi @Jakub. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Based on the information provided in the knowledge sources, I can offer some guidance on using the
ThemedLayoutV2
component with Next.js and Ant Design in refine. However, the specific issue you're encountering with duplicate layouts is not directly addressed in the provided documentation. Let me explain the recommended approach and suggest some potential solutions:
1. Using ThemedLayoutV2
in Next.js:
The recommended way to use ThemedLayoutV2
in a Next.js app with refine is slightly different from what you've implemented. According to the Next.js usage documentation, you should structure your app like this:
2. Customizing ThemedLayoutV2
:
If you need to customize the Sider
component, you can use the swizzle feature as mentioned in the Customizing with swizzle documentation. After swizzling, you can modify the generated components in your src/components/themedLayout
directory.
3. Server Component Issue:
The error you're getting about not being able to use hooks in a server component is likely because Next.js 13+ uses React Server Components by default. To resolve this, you can create a client component wrapper as you did, but you might need to adjust where you place the ThemedLayoutV2
.
4. Potential Solution:
Instead of wrapping your entire app with ThemedLayoutV2
, try using it in your page components or in a layout component that wraps your pages. Here's a potential structure:
This structure separates the Refine context from the themed layout, which might help avoid the duplicate layout issue you're experiencing.
If you're still encountering issues after trying these suggestions, it might be helpful to check the official refine GitHub repository for any open issues related to Next.js 13+ and ThemedLayoutV2
, or consider opening a new issue with your specific use case.
Remember, when customizing the layout, you can pass custom components to ThemedLayoutV2
as props, as shown in the usage documentation:
This allows you to customize individual parts of the layout without duplicating the entire structure.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@1212152033214464112> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
inland-turquoiseOP•3w ago
For anybody else looking for the solution. Use basically the solutions from 4. Or you can simply pass the Sider component to the default <ThemedLayoutV2> from
@refinedev/antd
. So your e.g. src/app/blog-posts/layout.tsx would look like this:
This works for me. The documentation is slightly misleading I would say.other-emerald•3w ago
Hello @Jakub, sorry for your trouble, Nextjs works diffrent from normal react application. Actually this code is from our examples, you can see here https://github.com/refinedev/refine/blob/main/examples/with-nextjs/src/app/blog-posts/layout.tsx
GitHub
refine/examples/with-nextjs/src/app/blog-posts/layout.tsx at main ·...
A React Framework for building internal tools, admin panels, dashboards & B2B apps with unmatched flexibility. - refinedev/refine