antd-mobile
Version:
<img src="https://gw.alipayobjects.com/mdn/rms_ee68a8/afts/img/A*hjjDS5Yy-ooAAAAAAAAAAAAAARQnAQ" alt="logo" width="100%" />
20 lines (19 loc) • 519 B
JavaScript
import { useIsomorphicLayoutEffect, useMemoizedFn } from 'ahooks';
export function useResizeEffect(effect, targetRef) {
const fn = useMemoizedFn(effect);
useIsomorphicLayoutEffect(() => {
const target = targetRef.current;
if (!target) return;
if (window.ResizeObserver) {
const observer = new ResizeObserver(() => {
fn(target);
});
observer.observe(target);
return () => {
observer.disconnect();
};
} else {
fn(target);
}
}, [targetRef]);
}