Skip to content

Commit

Permalink
fix: Remove console.warn in SSR (#1030)
Browse files Browse the repository at this point in the history
* fix: remove conditional warning

* chore: remove unnecessary tests

Co-authored-by: Qiushi Pan <17402261+qqhann@users.noreply.github.com>
  • Loading branch information
qqpann and qqpann authored Jun 30, 2022
1 parent 10c5d43 commit 84d1828
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 40 deletions.
12 changes: 0 additions & 12 deletions src/__tests__/generalSsrTests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ 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";
Expand All @@ -23,16 +21,6 @@ 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);
Expand Down
22 changes: 8 additions & 14 deletions src/hooks/useOnWindowResize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,14 @@ function useOnWindowResize(
when: boolean = true,
isLayoutEffect: boolean = false
) {
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."
);
}
useGlobalObjectEventListener(
window,
"resize",
callback,
{ passive: true },
when,
isLayoutEffect
);
}

export { useOnWindowResize };
22 changes: 8 additions & 14 deletions src/hooks/useOnWindowScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,14 @@ function useOnWindowScroll(
when: boolean = true,
isLayoutEffect: boolean = false
): void {
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."
);
}
useGlobalObjectEventListener(
window,
"scroll",
callback,
{ passive: true },
when,
isLayoutEffect
);
}

export { useOnWindowScroll };

0 comments on commit 84d1828

Please sign in to comment.