From cce673058d417bcb2b07b691696b09d0c05829ad Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Thu, 25 Apr 2024 08:21:31 -0700 Subject: [PATCH] maintain correct content offset when scroll view is suspended (#44256) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/44256 ## Changelog: [iOS] [Fixed] - Preserve content offset in ScrollView when the component is suspended # The problem On iOS, components are recycled. For ScrollView, its content offset has to be reset back to original position. When we call `[UIScrollView setContentOffset:]`, it triggers all of its delegate methods and triggers `scrollViewDidScroll` where we set native state. So when user scrolls to position 100 and scroll view suspends. it is removed from view hierarchy and recycled. Once the suspense boundary is resolved, scroll view will be inserted back into view hierarchy. But when it was recycled, we set back its original content offset (the default is 0, 0) but this was accidentally propagated through to shadow tree. # Solution To avoid this, we simply need to invalidate `_state` before calling `[UIScrollView setContentOffset:]`. Reviewed By: cipolleschi Differential Revision: D56573370 --- .../ComponentViews/ScrollView/RCTScrollViewComponentView.mm | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm index f08bde9ff265b1..83511c3afa1d2f 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm @@ -419,6 +419,10 @@ - (void)_updateStateWithContentOffset - (void)prepareForRecycle { [super prepareForRecycle]; + // Must invalidate state before setting contentOffset on ScrollView. + // Otherwise the state will be propagated to shadow tree. + _state.reset(); + const auto &props = static_cast(*_props); _scrollView.contentOffset = RCTCGPointFromPoint(props.contentOffset); // We set the default behavior to "never" so that iOS @@ -426,7 +430,6 @@ - (void)prepareForRecycle // and keeps it as an opt-in behavior. _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; _shouldUpdateContentInsetAdjustmentBehavior = YES; - _state.reset(); _isUserTriggeredScrolling = NO; CGRect oldFrame = self.frame; self.frame = CGRectZero;