Skip to content

Commit 9e85b7a

Browse files
vonovakfacebook-github-bot
authored andcommitted
ScrollView, HorizontalScrollView: do not ignore null contentOffset prop (#28760)
Summary: motivation: I was just checking out 30cc158 and noticed that the commit, I believe, is missing logic for when `contentOffset` is actually `null`. That is, consider you render `ScrollView` with `contentOffset` { x: 0, y: 100 } and then change that to null / undefined. I'd expect the content offset to invalidate (set to 0 - hope that's the default). ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://github.com/facebook/react-native/wiki/Changelog --> [Android] [Fixed] - ScrollView, HorizontalScrollView: do not ignore `null` `contentOffset` prop Pull Request resolved: #28760 Test Plan: Tested locally within RNTester <details><summary>code</summary> <p> ```js const Ex = () => { let _scrollView: ?React.ElementRef<typeof ScrollView> = React.useRef(null); const [offset, setOffset] = React.useState({x: 0, y: 20}); setTimeout(() => { setOffset(undefined); }, 2000); return ( <View> <ScrollView ref={_scrollView} automaticallyAdjustContentInsets={false} onScroll={() => { console.log('onScroll!'); }} contentOffset={offset} scrollEventThrottle={200} style={styles.scrollView}> {ITEMS.map(createItemRow)} </ScrollView> <Button label="Scroll to top" onPress={() => { nullthrows(_scrollView.current).scrollTo({y: 0}); }} /> <Button label="Scroll to bottom" onPress={() => { nullthrows(_scrollView.current).scrollToEnd({animated: true}); }} /> <Button label="Flash scroll indicators" onPress={() => { nullthrows(_scrollView.current).flashScrollIndicators(); }} /> </View> ); }; ``` </p> </details> Reviewed By: shergin Differential Revision: D22298676 Pulled By: JoshuaGross fbshipit-source-id: e411ba4c8a276908e354d59085d164a38ae253c0
1 parent 188a66d commit 9e85b7a

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollViewManager.java

+2
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ public void setContentOffset(ReactHorizontalScrollView view, ReadableMap value)
307307
double x = value.hasKey("x") ? value.getDouble("x") : 0;
308308
double y = value.hasKey("y") ? value.getDouble("y") : 0;
309309
view.reactScrollTo((int) PixelUtil.toPixelFromDIP(x), (int) PixelUtil.toPixelFromDIP(y));
310+
} else {
311+
view.reactScrollTo(0, 0);
310312
}
311313
}
312314
}

ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java

+2
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ public void setContentOffset(ReactScrollView view, ReadableMap value) {
311311
double x = value.hasKey("x") ? value.getDouble("x") : 0;
312312
double y = value.hasKey("y") ? value.getDouble("y") : 0;
313313
view.reactScrollTo((int) PixelUtil.toPixelFromDIP(x), (int) PixelUtil.toPixelFromDIP(y));
314+
} else {
315+
view.reactScrollTo(0, 0);
314316
}
315317
}
316318

0 commit comments

Comments
 (0)