ReferenceError: window is not defined
Answered
English Angora posted this in #help-forum
English AngoraOP
I have a client component, and I have this onClick function on one of the element:
But I'm still getting
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.
9 Replies
@English Angora 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.
try to import your component like this:
Then there shouldn't be an error
const ComponentC = dynamic(() => import('../components/C'), { ssr: false })Then there shouldn't be an error
@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,
});
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 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,
});
would you mind sharing a reproduction repo via jsfiddle or https://codesandbox.io/ ?
@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:
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