Skip to content

Commit

Permalink
fix(useViewportEffect): support changing recalculateLayoutBeforeUpdat…
Browse files Browse the repository at this point in the history
…e 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
  • Loading branch information
garthenweb committed Feb 16, 2020
1 parent 725caea commit 02efe83
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ export function useViewportEffect<T>(
notifyDimensions: () => !memoOptions.disableDimensionsUpdates,
notifyOnlyWhenIdle: () => Boolean(memoOptions.deferUpdateUntilIdle),
priority: () => memoOptions.priority || 'normal',
recalculateLayoutBeforeUpdate: memoOptions.recalculateLayoutBeforeUpdate,
recalculateLayoutBeforeUpdate: (...args: any) =>
memoOptions.recalculateLayoutBeforeUpdate
? memoOptions.recalculateLayoutBeforeUpdate(...args)
: null,
});
return () => removeViewportChangeListener(handleViewportChange);
}, [
Expand Down

0 comments on commit 02efe83

Please sign in to comment.