"window is not defined" - SSR support
See original GitHub issueI received this error when attempting to retrieve the value from a selector. This occurs in a Next.js 9.4 project, which is inherently server side rendered.
Looking through the source code it looks like there are a few references to the window object that aren’t available in SSR. Are there any plans to support SSR? I tried to work around it by checking for the presence of the window
object but because I’m trying to conditionally use state it breaks the rules of hooks 😦
ReferenceError: window is not defined
at r (C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:712)
at C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:1276
at e (C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:1290)
at C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:19654
at i (C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:19929)
at Object.c [as get] (C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:19966)
at x (C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:4554)
at C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:6719
at Object.replaceState (C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:10416)
at C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:6684
at Object.trace (C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:4312)
at getRecoilValueAsLoadable (C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:6652)
at n (C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:13390)
at r (C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:13682)
at Object.getRecoilState (C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:13775)
at useRecoilState (C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:14988)
Example consumer code:
export const projectNameState = selector({
key: "projectName",
get: ({ get }) => ...
});
export const ProjectNameProvider = () => {
const [projectName, setProjectName] = useRecoilState(projectNameState);
return (
<ProjectNameComponent
projectName={projectName}
setProjectName={setProjectName}
/>
);
};
Issue Analytics
- State:
- Created 3 years ago
- Reactions:9
- Comments:11 (2 by maintainers)
Top Results From Across the Web
React JS Server side issue - window not found - Stack Overflow
BROWSER to my advantage because it will be defined as undefined if it is server side, and it will be true if the...
Read more >window is not defined, when code render on server SSR ...
I'm using Next.js and there is no window when doing server side rendering.
Read more >SSR issue: window not defined - Sitecore Stack Exchange
The window object is not available during server side rendering because Node.js does not have a window object - it is a browser...
Read more >How to solve Next.js window is not defined
Solve a the common ReferenceError: next.js window is not defined error that you may see when doing SSR & pre-rendering.
Read more >How to Fix "window is not defined" in Next.js - Upmostly
This issue has to do with Server-Side Rendering in Next.js. Next.js will by default try to use SSR for your site. This means...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We plan to support SSR but haven’t really needed to try it yet. If you want to fix these window references that PR would be greatly appreciated.
For now, I’ll downgrade to 0.0.7.