From 9e85b7ad889900cd57cd2f82286aa8e034b0a32b Mon Sep 17 00:00:00 2001 From: Vojtech Novak Date: Tue, 30 Jun 2020 12:49:40 -0700 Subject: [PATCH] ScrollView, HorizontalScrollView: do not ignore `null` `contentOffset` prop (#28760) Summary: motivation: I was just checking out https://github.com/facebook/react-native/commit/30cc158a875a0414cf53d4d5155410eea5d5aeea 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 [Android] [Fixed] - ScrollView, HorizontalScrollView: do not ignore `null` `contentOffset` prop Pull Request resolved: https://github.com/facebook/react-native/pull/28760 Test Plan: Tested locally within RNTester
code

```js const Ex = () => { let _scrollView: ?React.ElementRef = React.useRef(null); const [offset, setOffset] = React.useState({x: 0, y: 20}); setTimeout(() => { setOffset(undefined); }, 2000); return ( { console.log('onScroll!'); }} contentOffset={offset} scrollEventThrottle={200} style={styles.scrollView}> {ITEMS.map(createItemRow)}

Reviewed By: shergin Differential Revision: D22298676 Pulled By: JoshuaGross fbshipit-source-id: e411ba4c8a276908e354d59085d164a38ae253c0 --- .../react/views/scroll/ReactHorizontalScrollViewManager.java | 2 ++ .../com/facebook/react/views/scroll/ReactScrollViewManager.java | 2 ++ 2 files changed, 4 insertions(+) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollViewManager.java index 7f11c77abf3a22..8b7215532a6424 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollViewManager.java @@ -307,6 +307,8 @@ public void setContentOffset(ReactHorizontalScrollView view, ReadableMap value) double x = value.hasKey("x") ? value.getDouble("x") : 0; double y = value.hasKey("y") ? value.getDouble("y") : 0; view.reactScrollTo((int) PixelUtil.toPixelFromDIP(x), (int) PixelUtil.toPixelFromDIP(y)); + } else { + view.reactScrollTo(0, 0); } } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java index adf04118c3f437..8e6f93ec28dd90 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java @@ -311,6 +311,8 @@ public void setContentOffset(ReactScrollView view, ReadableMap value) { double x = value.hasKey("x") ? value.getDouble("x") : 0; double y = value.hasKey("y") ? value.getDouble("y") : 0; view.reactScrollTo((int) PixelUtil.toPixelFromDIP(x), (int) PixelUtil.toPixelFromDIP(y)); + } else { + view.reactScrollTo(0, 0); } }