Skip to content

Commit 02efe83

Browse files
committed
fix(useViewportEffect): support changing recalculateLayoutBeforeUpdate in combination with deferUpdateUntilIdle
When recalculateLayoutBeforeUpdate is used with a dynamic value in a function, e.g. useRect, the function is not reassninged to the listener. This can especially become an issue with deferUpdateUntilIdle. In the example of useRect, the deferred function will be called with the old element (which might be null) and the update which is triggered afterwards containing the new function is not respected any longer. Afterwards the dependency is not changing any longer, therefore the state stays undefined. Using a dynamic function that calls the latest function independent of when it was registered fixes the issue. Closes #16
1 parent 725caea commit 02efe83

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

lib/hooks.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ export function useViewportEffect<T>(
6363
notifyDimensions: () => !memoOptions.disableDimensionsUpdates,
6464
notifyOnlyWhenIdle: () => Boolean(memoOptions.deferUpdateUntilIdle),
6565
priority: () => memoOptions.priority || 'normal',
66-
recalculateLayoutBeforeUpdate: memoOptions.recalculateLayoutBeforeUpdate,
66+
recalculateLayoutBeforeUpdate: (...args: any) =>
67+
memoOptions.recalculateLayoutBeforeUpdate
68+
? memoOptions.recalculateLayoutBeforeUpdate(...args)
69+
: null,
6770
});
6871
return () => removeViewportChangeListener(handleViewportChange);
6972
}, [

0 commit comments

Comments
 (0)