daily-toolset
Version:
A lightweight, versatile collection of TypeScript utility functions for everyday development needs. Simplify and streamline your Node.js, React, and Next.js projects with a powerful suite of well-organized helpers for strings, arrays, dates, objects, and
35 lines (34 loc) • 1.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useWindowSize = useWindowSize;
const react_1 = require("react");
/**
* useWindowSize
*
* A hook to get the current window size.
*
* It returns an object with two properties:
* - width: the current window width.
* - height: the current window height.
*
* The returned value is updated whenever the window is resized.
*
* @returns {Object} an object with the current window size.
*/
function useWindowSize() {
const [windowSize, setWindowSize] = (0, react_1.useState)({
width: window.innerWidth,
height: window.innerHeight,
});
(0, react_1.useEffect)(() => {
function handleResize() {
setWindowSize({
width: window.innerWidth,
height: window.innerHeight,
});
}
window.addEventListener("resize", handleResize);
return () => window.removeEventListener("resize", handleResize);
}, []);
return windowSize;
}