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

19 lines
435 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);
}
}, [isWindowClient]);
}