Skip to content

Commit c8166b0

Browse files
committed
fix: retry function target observe on next render
1 parent 9938c0d commit c8166b0

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/useResizeObserver.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,15 @@ export default function useResizeObserver(
6464

6565
// Dynamic observe
6666
const isFuncTarget = typeof getTarget === 'function';
67+
const funcTargetIdRef = React.useRef(0);
68+
6769
React.useEffect(() => {
6870
const target = isFuncTarget ? getTarget() : getTarget;
6971

7072
if (target && enabled) {
7173
observe(target, onInternalResize);
74+
} else if (enabled && isFuncTarget) {
75+
funcTargetIdRef.current += 1;
7276
}
7377

7478
return () => {
@@ -78,7 +82,8 @@ export default function useResizeObserver(
7882
};
7983
}, [
8084
enabled,
81-
// When is function, no need to watch it
82-
isFuncTarget ? 0 : getTarget,
85+
// If function target resolves after a parent render, the bumped ref value
86+
// lets the next render re-run this effect without watching the function identity.
87+
isFuncTarget ? funcTargetIdRef.current : getTarget,
8388
]);
8489
}

0 commit comments

Comments
 (0)