Skip to content

Commit 305249f

Browse files
sammy-SCcipolleschi
authored andcommitted
maintain correct content offset when scroll view is suspended (#44256)
Summary: Pull Request resolved: #44256 [iOS] [Fixed] - Preserve content offset in ScrollView when the component is suspended 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. To avoid this, we simply need to invalidate `_state` before calling `[UIScrollView setContentOffset:]`. Reviewed By: cipolleschi Differential Revision: D56573370 fbshipit-source-id: c03d7d2d403af2e1649b4cf189072baeb4c286c8
1 parent 3467f2f commit 305249f

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,14 +420,18 @@ - (void)_updateStateWithContentOffset
420420

421421
- (void)prepareForRecycle
422422
{
423+
[super prepareForRecycle];
424+
// Must invalidate state before setting contentOffset on ScrollView.
425+
// Otherwise the state will be propagated to shadow tree.
426+
_state.reset();
427+
423428
const auto &props = static_cast<const ScrollViewProps &>(*_props);
424429
_scrollView.contentOffset = RCTCGPointFromPoint(props.contentOffset);
425430
// We set the default behavior to "never" so that iOS
426431
// doesn't do weird things to UIScrollView insets automatically
427432
// and keeps it as an opt-in behavior.
428433
_scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
429434
_shouldUpdateContentInsetAdjustmentBehavior = YES;
430-
_state.reset();
431435
_isUserTriggeredScrolling = NO;
432436
CGRect oldFrame = self.frame;
433437
self.frame = CGRectZero;

0 commit comments

Comments
 (0)