mirror of
https://github.com/zoriya/react-native-web.git
synced 2026-06-03 10:46:19 +00:00
[fix] Avoid needing to memoize onLayout callbacks
If 'onLayout' is an inline function, it could cause the DOM node to enter a cycle of being observed/unobserved with the result that 'onLayout' was constantly called. Fix #1704
This commit is contained in:
+16
-7
@@ -61,23 +61,32 @@ function getResizeObserver(): ?ResizeObserver {
|
|||||||
|
|
||||||
export default function useElementLayout(
|
export default function useElementLayout(
|
||||||
ref: ElementRef<any>,
|
ref: ElementRef<any>,
|
||||||
onLayout?: (e: LayoutEvent) => void
|
onLayout?: ?(e: LayoutEvent) => void
|
||||||
) {
|
) {
|
||||||
const observer = getResizeObserver();
|
const observer = getResizeObserver();
|
||||||
|
|
||||||
useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
||||||
const node = ref.current;
|
const node = ref.current;
|
||||||
if (node != null && observer != null && typeof onLayout === 'function') {
|
if (node != null) {
|
||||||
observer.observe(node);
|
|
||||||
// $FlowFixMe
|
|
||||||
node[DOM_LAYOUT_HANDLER_NAME] = onLayout;
|
node[DOM_LAYOUT_HANDLER_NAME] = onLayout;
|
||||||
}
|
}
|
||||||
|
}, [ref, onLayout]);
|
||||||
|
|
||||||
|
// Observing is done in a separate effect to avoid this effect running
|
||||||
|
// when 'onLayout' changes.
|
||||||
|
useLayoutEffect(() => {
|
||||||
|
const node = ref.current;
|
||||||
|
if (node != null && observer != null) {
|
||||||
|
if (typeof node[DOM_LAYOUT_HANDLER_NAME] === 'function') {
|
||||||
|
observer.observe(node);
|
||||||
|
} else {
|
||||||
|
observer.unobserve(node);
|
||||||
|
}
|
||||||
|
}
|
||||||
return () => {
|
return () => {
|
||||||
if (node != null && observer != null) {
|
if (node != null && observer != null) {
|
||||||
observer.unobserve(node);
|
observer.unobserve(node);
|
||||||
// $FlowFixMe
|
|
||||||
delete node[DOM_LAYOUT_HANDLER_NAME];
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}, [ref, onLayout, observer]);
|
}, [ref, observer]);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user