Use commands instead of setNativeProps for AndroidSwipeRefreshLayout

Summary:
As a part of the migration from setNativeProps in Fabric and Paper, we are replacing it by view commands in the RefreshControl component on Android.

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D18475450

fbshipit-source-id: ad89547fada3444f725fd9b00e8482cfc8f4d7fc
This commit is contained in:
Oleksandr Melnykov
2020-01-14 03:53:57 -08:00
committed by Facebook Github Bot
parent 461d68e656
commit 1e6e37a3a8
2 changed files with 25 additions and 17 deletions
@@ -15,8 +15,9 @@ const React = require('react');
import type {ColorValue} from '../../StyleSheet/StyleSheetTypes';
import type {ViewProps} from '../View/ViewPropTypes';
import AndroidSwipeRefreshLayoutNativeComponent from './AndroidSwipeRefreshLayoutNativeComponent';
import AndroidSwipeRefreshLayoutNativeComponent, {
Commands as AndroidSwipeRefreshLayoutCommands,
} from './AndroidSwipeRefreshLayoutNativeComponent';
import PullToRefreshViewNativeComponent, {
Commands as PullToRefreshCommands,
} from './PullToRefreshViewNativeComponent';
@@ -138,11 +139,12 @@ export type RefreshControlProps = $ReadOnly<{|
class RefreshControl extends React.Component<RefreshControlProps> {
static SIZE: any = RefreshLayoutConsts.SIZE;
_setNativePropsOnRef: ?({refreshing: boolean, ...}) => void;
_nativeRef: ?React.ElementRef<
| typeof PullToRefreshViewNativeComponent
| typeof AndroidSwipeRefreshLayoutNativeComponent,
>;
_lastNativeRefreshing = false;
_nativeRef: ?React.ElementRef<typeof PullToRefreshViewNativeComponent>;
componentDidMount() {
this._lastNativeRefreshing = this.props.refreshing;
}
@@ -155,13 +157,14 @@ class RefreshControl extends React.Component<RefreshControlProps> {
this._lastNativeRefreshing = this.props.refreshing;
} else if (
this.props.refreshing !== this._lastNativeRefreshing &&
this._setNativePropsOnRef
this._nativeRef
) {
if (Platform.OS === 'android') {
this._setNativePropsOnRef({
refreshing: this.props.refreshing,
});
} else if (this._nativeRef) {
AndroidSwipeRefreshLayoutCommands.setNativeRefreshing(
this._nativeRef,
this.props.refreshing,
);
} else {
PullToRefreshCommands.setNativeRefreshing(
this._nativeRef,
this.props.refreshing,
@@ -172,11 +175,6 @@ class RefreshControl extends React.Component<RefreshControlProps> {
}
render(): React.Node {
const setRef = ref => {
this._setNativePropsOnRef = ref ? ref.setNativeProps.bind(ref) : null;
this._nativeRef = ref;
};
if (Platform.OS === 'ios') {
const {
enabled,
@@ -189,7 +187,7 @@ class RefreshControl extends React.Component<RefreshControlProps> {
return (
<PullToRefreshViewNativeComponent
{...props}
ref={setRef}
ref={this._setNativeRef}
onRefresh={this._onRefresh}
/>
);
@@ -198,7 +196,7 @@ class RefreshControl extends React.Component<RefreshControlProps> {
return (
<AndroidSwipeRefreshLayoutNativeComponent
{...props}
ref={setRef}
ref={this._setNativeRef}
onRefresh={this._onRefresh}
/>
);
@@ -214,6 +212,15 @@ class RefreshControl extends React.Component<RefreshControlProps> {
// make sure it stays in sync with the js component.
this.forceUpdate();
};
_setNativeRef = (
ref: ?React.ElementRef<
| typeof PullToRefreshViewNativeComponent
| typeof AndroidSwipeRefreshLayoutNativeComponent,
>,
) => {
this._nativeRef = ref;
};
}
module.exports = RefreshControl;