Skip to content

Commit

Permalink
fix: memoized getSize to fix "Maximum update depth exceeded" error
Browse files Browse the repository at this point in the history
  • Loading branch information
noahgerard committed Sep 3, 2024
1 parent e39082b commit 7b0d4d9
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/app/hooks/useWindowSize.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { useEffect, useState } from "react";
import { useCallback, useEffect, useState } from "react";

export default function useWindowSize() {
const isClient = typeof window === "object";

function getSize() {
if (typeof window !== "undefined") {
return {
width: window.innerWidth || 0,
height: window.innerHeight || 0,
};
}
return {
width: 0,
height: 0,
};
}
const getSize = useCallback(() => {
if (isClient) {
return {
width: window.innerWidth || 0,
height: window.innerHeight || 0,
};
}
return {
width: 0,
height: 0,
};
}, [isClient]);

const [windowSize, setWindowSize] = useState(getSize);

Expand Down

0 comments on commit 7b0d4d9

Please sign in to comment.