lbry-desktop/ui/effects/use-on-resize.js

20 lines
495 B
JavaScript
Raw Normal View History

import { useEffect } from 'react';
import debounce from 'util/debounce';
export function useOnResize(cb) {
2022-01-31 13:34:44 +01:00
const isWindowClient = typeof window === 'object';
useEffect(() => {
const handleResize = debounce(cb, 100);
2022-01-31 13:34:44 +01:00
if (isWindowClient) {
window.addEventListener('resize', handleResize);
2022-01-31 13:34:44 +01:00
handleResize();
2022-01-31 13:34:44 +01:00
return () => window.removeEventListener('resize', handleResize);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
2022-01-31 13:34:44 +01:00
}, [isWindowClient]);
}