UNPKG

@jay19950328/react-hooks

Version:
40 lines (33 loc) 1.02 kB
import React from 'react'; function useWindowSize(_temp) { var _ref = _temp === void 0 ? {} : _temp, _ref$width = _ref.width, width = _ref$width === void 0 ? Infinity : _ref$width, _ref$height = _ref.height, height = _ref$height === void 0 ? Infinity : _ref$height; var isClient = typeof window === 'object'; var _React$useState = React.useState({ width: isClient ? window.innerWidth : width, height: isClient ? window.innerHeight : height }), state = _React$useState[0], setState = _React$useState[1]; React.useEffect(function () { if (isClient) { var onResize = function onResize() { setState({ width: window.innerWidth, height: window.innerHeight }); }; window.addEventListener('resize', onResize); return function () { return window.removeEventListener('resize', onResize); }; } else { return undefined; } }, []); return state; } export default useWindowSize;