@zohodesk/hooks
Version:
Unified Component Library - Hooks
23 lines (22 loc) • 854 B
JavaScript
// Window resize: https://javascript.plainenglish.io/how-to-watch-the-javascript-window-resize-event-7637acecd036
// We cann't observe window. Bcoz, getting below error:
// Uncaught TypeError: Failed to execute 'observe' on 'ResizeObserver': parameter 1 is not of type 'Element'.
// So we can use html or body element. Incase that html or body is not having height in some cases, then that resize will not work correctly.
// So useWindowResize hook is implemented
import { useEffect } from 'react';
export default function useWindowResize(_ref) {
let {
isObserve = true,
callback
} = _ref;
useEffect(() => {
if (typeof callback !== 'function' || !isObserve) {
return;
}
;
window.addEventListener('resize', callback);
return () => {
window.removeEventListener('resize', callback);
};
}, [isObserve]);
}