I wrote a component that looks like this:
'use client'
export const HorizontalModule = (props: any) => {
...
return (
{scrollPosition >= 0 && (
<FirstModule />
)}
{scrollPosition >= window.innerHeight * 2 && (
<SecondModule />
)}
)
})
But I got the "window is not defined" error.
Reading through different posts, I have found that most people found using dynamic importing useful so I did this in the parent component which is a nextjs page:
const HorizontalModule = dynamic<any>(
() => import('./HorizontalModule/HorizontalModule').then((mod) => mod.HorizontalModule),
{
ssr: false,
suspense: true,
loading: () => <p>Loading...</p>
}
)
At first I was getting this error: "Object is not a function"
Now I'm getting "Unsupported Server Component type: undefined"
I don't exactly know what I did to switch the error but it is still not working.
I gotta mention, I use the window object all over the HorizontalModule code, in different useEffects
but when I use it inside the render function, all stops working.
I also tried writing inside the component a validation like this:
if (window === undefined) return (<></>)
return (...)
I got the same window undefined object or a hydration error.
I don't know what else is there to do, ssr false doesn't work, suspense either, window condition...