Next.js Discord

Discord Forum

ReferenceError: window is not defined

Answered
English Angora posted this in #help-forum
Open in Discord
English AngoraOP
I have a client component, and I have this onClick function on one of the element:
            onClick={() =>
              typeof window !== 'undefined' &&
              window.open('https://www.example.com', '_blank')
            }

But I'm still getting
reference error
during build.
Answered by English Angora
Thanks but turns out, I tried to access window object outside useEffect in some other component, nothing to do with dynamic import.
View full answer

9 Replies

@B33fb0n3 try to import your component like this: tsx const ComponentC = dynamic(() => import('../components/C'), { ssr: false }) Then there shouldn't be an error
English AngoraOP
Still error, or am i doing it wrong?

page.tsx -Server Component/Page
<Hero/> - Also Server Compoent

an inside Hero I have this client component "XYZ" that uses window, imported like this in Hero:
const XYZ= dynamic(() => import('./Component/XYZ'), {
ssr: false,
});
@English Angora https://codesandbox.io/p/sandbox/referennce-error-q8zjjt
it seems like you haven't set up the repo correctly. Please provide a repo, that only shows the mentioned error
English AngoraOP
I'm not sure what you're trying to see but the error literally is this:
@English Angora I'm not sure what you're trying to see but the error literally is this:
the codesandbox provided is just a template react app, not even a nextjs app
so yeah, what they meant is that you should provide them with a codesandbox/reproduction repository of a code that reproduces the error so they can check and test locally
English AngoraOP
Thanks but turns out, I tried to access window object outside useEffect in some other component, nothing to do with dynamic import.
Answer