From b5c723a9c13d21d15a700fba182e75b6c4bcabd7 Mon Sep 17 00:00:00 2001 From: Bhargav Ponnapalli Date: Sat, 2 Jul 2022 23:48:04 +0530 Subject: [PATCH] revert: "fix: Remove console.warn in SSR (#1030)" (#1136) This reverts commit 84d18280f92460b9ce7791af6acb105adc1a85f2. --- src/__tests__/generalSsrTests.spec.ts | 12 ++++++++++++ src/hooks/useOnWindowResize.ts | 22 ++++++++++++++-------- src/hooks/useOnWindowScroll.ts | 22 ++++++++++++++-------- 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/src/__tests__/generalSsrTests.spec.ts b/src/__tests__/generalSsrTests.spec.ts index e34688f8f2..dc4ef9f06c 100644 --- a/src/__tests__/generalSsrTests.spec.ts +++ b/src/__tests__/generalSsrTests.spec.ts @@ -6,6 +6,8 @@ import { useFullscreen } from "../hooks/useFullscreen"; import { useIntervalWhen } from "../hooks/useIntervalWhen"; import { useLocalstorage } from "../hooks/useLocalstorage"; import { useLocalstorageState } from "../hooks/useLocalstorageState"; +import { useOnWindowResize } from "../hooks/useOnWindowResize"; +import { useOnWindowScroll } from "../hooks/useOnWindowScroll"; import { useOnline } from "../hooks/useOnline"; import { useSessionstorage } from "../hooks/useSessionstorage"; import { useThrottle } from "../hooks/useThrottle"; @@ -21,6 +23,16 @@ describe("when window is undefined", () => { consoleSpy.mockClear(); }); + it("useOnWindowResize logs warning", () => { + renderHook(() => useOnWindowResize(mockCallback)); + expect(consoleSpy).toHaveBeenCalledTimes(1); + }); + + it("useOnWindowScroll logs warning", () => { + renderHook(() => useOnWindowScroll(mockCallback)); + expect(consoleSpy).toHaveBeenCalledTimes(1); + }); + it("useIntervalWhen logs warning", () => { renderHook(() => useIntervalWhen(mockCallback)); expect(consoleSpy).toHaveBeenCalledTimes(1); diff --git a/src/hooks/useOnWindowResize.ts b/src/hooks/useOnWindowResize.ts index 96d55cad8e..764b340518 100644 --- a/src/hooks/useOnWindowResize.ts +++ b/src/hooks/useOnWindowResize.ts @@ -15,14 +15,20 @@ function useOnWindowResize( when: boolean = true, isLayoutEffect: boolean = false ) { - useGlobalObjectEventListener( - window, - "resize", - callback, - { passive: true }, - when, - isLayoutEffect - ); + if (typeof window !== "undefined") { + useGlobalObjectEventListener( + window, + "resize", + callback, + { passive: true }, + when, + isLayoutEffect + ); + } else { + console.warn( + "useOnWindowResize can't attach an event listener as window is undefined." + ); + } } export { useOnWindowResize }; diff --git a/src/hooks/useOnWindowScroll.ts b/src/hooks/useOnWindowScroll.ts index 9efe98e9ea..a9a3383e81 100644 --- a/src/hooks/useOnWindowScroll.ts +++ b/src/hooks/useOnWindowScroll.ts @@ -14,14 +14,20 @@ function useOnWindowScroll( when: boolean = true, isLayoutEffect: boolean = false ): void { - useGlobalObjectEventListener( - window, - "scroll", - callback, - { passive: true }, - when, - isLayoutEffect - ); + if (typeof window !== "undefined") { + useGlobalObjectEventListener( + window, + "scroll", + callback, + { passive: true }, + when, + isLayoutEffect + ); + } else { + console.warn( + "useOnWindowScroll can't attach an event listener as window is undefined." + ); + } } export { useOnWindowScroll };