Skip to content

Commit

Permalink
Add innerViewRef prop to ScrollView
Browse files Browse the repository at this point in the history
Summary:
Right now, people are calling `getInnerViewNode` and `getInnerViewRef` to get the ref of the `View` within `ScrollView`. Instead, this change adds a prop to `ScrollView` to give people direct access to that `View` if they need it.

Previous usage:
```
const myRef = React.createRef<React.ElementRef<typeof ScrollView>>();

<ScrollView ref={myRef} />

const innerViewRef = myRef.current.getInnerViewRef();

innerViewRef.measure();

```

New usage:
```
const myRef = React.createRef<React.ElementRef<typeof View>>();

<ScrollView innerViewRef={myRef} />

// now, myRef.current can be used directly as the ref
myRef.current.measure();
```

Changelog:
[Changed][General] ScrollView: Deprecate getInnerViewNode and getInnerViewRef methods. Use innerViewRef={myRef} prop instead.

Reviewed By: TheSavior

Differential Revision: D19713191

fbshipit-source-id: 3304cb94a253dafb458ef49d6331e0e432693431
  • Loading branch information
kacieb authored and facebook-github-bot committed Feb 11, 2020
1 parent 29d3dfb commit 10254a9
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions Libraries/Components/ScrollView/ScrollView.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const invariant = require('invariant');
const processDecelerationRate = require('./processDecelerationRate');
const resolveAssetSource = require('../../Image/resolveAssetSource');
const splitLayoutProps = require('../../StyleSheet/splitLayoutProps');
const setAndForwardRef = require('../../Utilities/setAndForwardRef');

import type {EdgeInsetsProp} from '../../StyleSheet/EdgeInsetsPropType';
import type {PointProp} from '../../StyleSheet/PointPropType';
Expand Down Expand Up @@ -575,6 +576,11 @@ export type Props = $ReadOnly<{|
// $FlowFixMe - how to handle generic type without existential operator?
refreshControl?: ?React.Element<any>,
children?: React.Node,
/**
* A ref to the inner View element of the ScrollView. This should be used
* instead of calling `getInnerViewRef`.
*/
innerViewRef?: React.Ref<typeof View>,
|}>;

type State = {|
Expand Down Expand Up @@ -766,10 +772,18 @@ class ScrollView extends React.Component<Props, State> {
}

getInnerViewNode(): ?number {
console.warn(
'`getInnerViewNode()` is deprecated. This will be removed in a future release. ' +
'Use <ScrollView innerViewRef={myRef} /> instead.',
);
return ReactNative.findNodeHandle(this._innerViewRef);
}

getInnerViewRef(): ?React.ElementRef<HostComponent<mixed>> {
getInnerViewRef(): ?React.ElementRef<typeof View> {
console.warn(
'`getInnerViewRef()` is deprecated. This will be removed in a future release. ' +
'Use <ScrollView innerViewRef={myRef} /> instead.',
);
return this._innerViewRef;
}

Expand Down Expand Up @@ -951,10 +965,13 @@ class ScrollView extends React.Component<Props, State> {
this._scrollViewRef = ref;
};

_innerViewRef: ?React.ElementRef<HostComponent<mixed>> = null;
_setInnerViewRef = (ref: ?React.ElementRef<HostComponent<mixed>>) => {
this._innerViewRef = ref;
};
_innerViewRef: ?React.ElementRef<typeof View> = null;
_setInnerViewRef = setAndForwardRef({
getForwardedRef: () => this.props.innerViewRef,
setLocalRef: ref => {
this._innerViewRef = ref;
},
});

render(): React.Node | React.Element<string> {
let ScrollViewClass;
Expand Down

0 comments on commit 10254a9

Please sign in to comment.