think/packages/client/src/hooks/useWindowSize.tsx

28 lines
606 B
TypeScript
Raw Normal View History

2022-02-20 11:51:55 +00:00
import { useState, useEffect } from "react";
interface Size {
width: number | undefined;
height: number | undefined;
}
export function useWindowSize(): Size {
const [windowSize, setWindowSize] = useState<Size>({
width: undefined,
height: undefined,
});
useEffect(() => {
function handleResize() {
setWindowSize({
width: window.innerWidth,
height: window.innerHeight,
});
}
window.addEventListener("resize", handleResize);
handleResize();
return () => window.removeEventListener("resize", handleResize);
}, []);
return windowSize;
}