Before diving in, let me show you what we’re are going to build today 🔥
Smooth right? Let’s build it.
1. Setup
The page.tsx file exports a server component by default. If you have tried working with framer motion before, you’ll probably be familiar with the following error:
This is because framer-motion uses functionality that supports only in a client component. The solution is very simple. We make a client component where we import motion tags and the animation presence, and pass everything else as a children.
1.1 The ClientAnimatePresence Component
Make a client-animate-presence.tsx file inside your components folder with the following code:
Now instead of the AnimatePresence component we will use our custom client animate presence component.
1.2 The ClientMotionDiv Component
Similarly, make a client-motion-div.tsx file inside your components folder with the following code:
1.3 The PageAnimation component
That’s it with the setup. It’s time to use animation to your pages.
2. Using the PageAnimation Component
The PageAnimation component now can be used in any server components inside the Nex.tjs (13 | 14) app folder. Here’s an example:
And that’s it! Hope you’ve enjoyed reading the post ^^.
If it helped you, help others by sharing the article!
You can also check the implementation on my own portfolio in this github repo.