@figliolia/react-hooks
Version:
A small collection of simple React Hooks you're probably rewriting on a regular basis
23 lines (22 loc) • 918 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useWindowSize = void 0;
const react_1 = require("react");
const getDimensions = () => {
var _a, _b;
return { width: (_a = window === null || window === void 0 ? void 0 : window.innerWidth) !== null && _a !== void 0 ? _a : 0, height: (_b = window === null || window === void 0 ? void 0 : window.innerHeight) !== null && _b !== void 0 ? _b : 0 };
};
const useWindowSize = () => {
const [dimensions, setDimensions] = (0, react_1.useState)(getDimensions());
const onResize = (0, react_1.useCallback)(() => {
setDimensions(getDimensions());
}, []);
(0, react_1.useLayoutEffect)(() => {
window.addEventListener("resize", onResize);
return () => {
window.removeEventListener("resize", onResize);
};
});
return dimensions;
};
exports.useWindowSize = useWindowSize;