Fix Animated Value initialized with undefined in ScrollView (#28349)

Summary:
When passing an object to contentOffset that doesn't have `y` prop set it causes the following error:

```
 Error: AnimatedValue: Attempting to set value to undefined

This error is located at:
    in ScrollView (at src/index.js:638)
...
```

This happens since a runtime check was added to the `AnimatedValue` constructor. (a3aaa471ec)

According to flow types the object passed to contentOffset should always contain both x and y props but since it worked before when y is undefined I think its fine to patch the runtime behaviour defensively, especially since the code change is simple.

## Changelog

[General] [Fixed] - Fix Animated Value initialized with undefined in ScrollView
Pull Request resolved: https://github.com/facebook/react-native/pull/28349

Test Plan: Tested that the crash no longer reproduces when passing an empty object to contentOffset.

Reviewed By: cpojer

Differential Revision: D20601664

Pulled By: hramos

fbshipit-source-id: b098a2dd1e702f995a9a92fa6e4e9a204187dac4
This commit is contained in:
Janic Duplessis
2020-03-24 04:07:40 -07:00
committed by Facebook GitHub Bot
parent 7dff0f43be
commit cf02bd9b76
@@ -716,11 +716,9 @@ class ScrollView extends React.Component<Props, State> {
UNSAFE_componentWillMount() {
this._scrollResponder.UNSAFE_componentWillMount();
this._scrollAnimatedValue = new AnimatedImplementation.Value(
this.props.contentOffset ? this.props.contentOffset.y : 0,
);
this._scrollAnimatedValue.setOffset(
this.props.contentInset ? this.props.contentInset.top || 0 : 0,
this.props.contentOffset?.y ?? 0,
);
this._scrollAnimatedValue.setOffset(this.props.contentInset?.top ?? 0);
this._stickyHeaderRefs = new Map();
this._headerLayoutYs = new Map();
}