antonioru / beautiful-react-hooks

🔥 A collection of beautiful and (hopefully) useful React hooks to speed-up your components and hooks development 🔥

Home Page:https://antonioru.github.io/beautiful-react-hooks/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

useGlobalEvent should check `isClient` before referencing `window`

santialbo opened this issue · comments

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;
commented

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