Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useGlobalEvent should check isClient before referencing window #365

Closed
santialbo opened this issue Jun 14, 2022 · 1 comment
Closed

useGlobalEvent should check isClient before referencing window #365

santialbo opened this issue Jun 14, 2022 · 1 comment

Comments

@santialbo
Copy link

santialbo commented Jun 14, 2022

Describe the bug
When SSR-ing a component that uses a global event hook like useWindowScroll, an error is thrown: ReferenceError: window is not defined. This is because useGlobalEvent, references the window object directly in here.

Expected behavior
Hooks using useGlobalEvent should do nothing on the server.

My patch-package file until fixed

diff --git a/node_modules/beautiful-react-hooks/useGlobalEvent.js b/node_modules/beautiful-react-hooks/useGlobalEvent.js
index 3bbf551..a77d01b 100644
--- a/node_modules/beautiful-react-hooks/useGlobalEvent.js
+++ b/node_modules/beautiful-react-hooks/useGlobalEvent.js
@@ -8,7 +8,7 @@ var useEvent_1 = __importDefault(require("./useEvent"));
  * Accepts an event name then returns a callback setter for a function to be performed when the event triggers.
  */
 var useGlobalEvent = function (eventName, opts) {
-    var target = { current: window }; // that's a bit of a hack but it works
+    var target = { current: typeof window === "undefined" ? undefined : null }; // that's a bit of a hack but it works
     return (0, useEvent_1.default)(target, eventName, opts);
 };
 exports.default = useGlobalEvent;
@antonioru
Copy link
Owner

@santialbo hello, thanks for pointing this out.
Last version fixes this bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants