Skip to content

Commit d865cc5

Browse files
lenaicNick
andauthored
Fix duplicate onScroll events breaking flatlist. (#1873) (#1881)
Co-authored-by: Nick <[email protected]>
1 parent 34d43ab commit d865cc5

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

React/Views/ScrollView/RCTScrollView.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ @implementation RCTScrollView {
382382
BOOL _allowNextScrollNoMatterWhat;
383383
#if TARGET_OS_OSX // [macOS
384384
BOOL _notifyDidScroll;
385+
NSPoint _lastScrollPosition;
385386
#endif // macOS]
386387
CGRect _lastClippedToRect;
387388
uint16_t _coalescingKey;
@@ -477,6 +478,7 @@ - (instancetype)initWithEventDispatcher:(id<RCTEventDispatcherProtocol>)eventDis
477478
#else // [macOS
478479
_scrollView.drawsBackground = NO;
479480
_scrollView.postsBoundsChangedNotifications = YES;
481+
_lastScrollPosition = NSZeroPoint;
480482
#endif // macOS]
481483

482484
#if !TARGET_OS_OSX // [macOS]
@@ -925,6 +927,22 @@ - (void)scrollViewDidScroll:(RCTCustomScrollView *)scrollView // [macOS]
925927
{
926928
NSTimeInterval now = CACurrentMediaTime();
927929
[self updateClippedSubviews];
930+
931+
#if TARGET_OS_OSX // [macOS
932+
/**
933+
* To check for effective scroll position changes, the comparison with lastScrollPosition should happen
934+
* after updateClippedSubviews. updateClippedSubviews will update the display of the vertical/horizontal
935+
* scrollers which can change the clipview bounds.
936+
* This change also ensures that no onScroll events are sent when the React setFrame call is running,
937+
* which could submit onScroll events while the content view was not setup yet.
938+
*/
939+
BOOL didScroll = !NSEqualPoints(scrollView.contentView.bounds.origin, _lastScrollPosition);
940+
if (!didScroll) {
941+
return;
942+
}
943+
_lastScrollPosition = scrollView.contentView.bounds.origin;
944+
#endif // macOS]
945+
928946
/**
929947
* TODO: this logic looks wrong, and it may be because it is. Currently, if _scrollEventThrottle
930948
* is set to zero (the default), the "didScroll" event is only sent once per scroll, instead of repeatedly

0 commit comments

Comments
 (0)