Hello everyone,
I'm writing about component creation and how to deal with client and server components. I have a problem. My header component needs an animation that requires it to be a client component. However, inside the header component, I have children that need to be server components. The issue is that I'm declaring my header component as a client component because of the Framer Motion animation I'm applying to it. I really don't know how to fix this issue and how to structure my code so I can have my header animation and still have the header child components be server components.
Can anyone help me resolve this? Thank you!
'use client'
import React, {useState} from "react";
import {motion, useMotionValueEvent, useScroll} from "framer-motion";
import HeaderTopBar from "@/components/layout/header/header-top-bar";
import HeaderBottomBar from "@/components/layout/header/header-bottom-bar";
export default function Header() {
const {scrollY} = useScroll();
const [hidden, setHidden] = useState(false);
useMotionValueEvent(scrollY, 'change', (latest) => {
const previous = scrollY.getPrevious();
if (previous !== undefined && latest > previous && latest > 150) {
setHidden(true);
} else {
setHidden(false);
}
});
return (
<motion.header
variants={{
visible: {y: 0},
hidden: {y: '-100%'}
}}
transition={{duration: 0.3, ease: 'easeInOut'}}
animate={hidden ? 'hidden' : 'visible'}
className="sticky top-0 z-50 w-screen bg-white"
>
<HeaderTopBar/>
<HeaderBottomBar/>
</motion.header>
)
}