Skip to content

Commit

Permalink
Make useWindowInnerSize not crash on the server
Browse files Browse the repository at this point in the history
  • Loading branch information
garronej committed Jul 7, 2023
1 parent 600bd42 commit ada6c9a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/tools/isBrowser.ts
@@ -0,0 +1 @@
export const isBrowser = typeof window === "object" && typeof document === "object";
6 changes: 4 additions & 2 deletions src/tools/useWindowInnerSize.ts
Expand Up @@ -3,12 +3,14 @@
import { useState } from "react";
import { Evt } from "evt";
import { useEvt } from "evt/hooks/useEvt";
import { isBrowser } from "./isBrowser";

/** Returns -1 values on the server side */
export function useWindowInnerSize() {

const [dimensions, setDimensions] = useState(() => ({
"windowInnerWidth": window.innerWidth,
"windowInnerHeight": window.innerHeight
"windowInnerWidth": !isBrowser ? -1 : window.innerWidth,
"windowInnerHeight": !isBrowser ? -1 : window.innerHeight
}));

useEvt(ctx =>
Expand Down
2 changes: 1 addition & 1 deletion src/useWindowInnerSize.ts
Expand Up @@ -2,7 +2,7 @@
import { useWindowInnerSize as useRealWindowInnerSize } from "./tools/useWindowInnerSize";
import { useViewPortState } from "./ViewPortAdapter";


/** Returns -1 values on the server side */
export function useWindowInnerSize() {

const { viewPortState } = useViewPortState();
Expand Down

0 comments on commit ada6c9a

Please sign in to comment.