Trying to use nextjs for building my landing page but facing issues with using both ssr and csr in same component.
I have a component navbar
import navbar_logo from "../../assets/Vector.svg";
import styles from "./Navbar.module.scss";
import scrollIntoSection from "../../utils/scrollIntoSection";
import HamburgerIcon from "../../assets/navbar/Hamburger_Icon.svg";
const Navbar: React.FC<NavbarProps> = ({ navbarItems }) => {
const [scrolled, setScrolled] = useState(false);
useEffect(() => {
const handleScroll = () => {
setScrolled(window.scrollY > document.querySelector("#hero").offsetHeight);
};
window.addEventListener("scroll", handleScroll);
return () => window.removeEventListener("scroll", handleScroll);
}, []);
return (
<nav className={styles.stickyNav}>
<div className="nav-container maxWidth_1200">
<img src={navbar_logo} alt="Company logo" className="nav-logo-img-container" />
<ul>
{navbarItems.map((item, idx) => (
<li key={idx} onClick={() => scrollIntoSection(item)}>{item}</li>
))}
</ul>
</div>
</nav>
);
};
type NavbarProps = { navbarItems: string[] };
export default Navbar;
When i try running this it shows some errors like: ``You're importing a component that needs useEffect. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.`
I expected next to handle ssr and csr implicitly, but if i decalre the component as "use client", that doesn't give the SEO benefits. Can someone suggest how do i use ssr and csr both at the same time.