UNPKG

@sdcx/pull-to-refresh

Version:
45 lines (44 loc) 1.68 kB
import React, { Component } from 'react'; import { findNodeHandle, UIManager } from 'react-native'; import PullToRefreshFooterNativeComponent from './PullToRefreshFooterNativeComponent'; const PullToRefreshFooterCommands = { setNativeRefreshing(componentOrHandle, refreshing) { UIManager.dispatchViewManagerCommand(findNodeHandle(componentOrHandle), 'setNativeRefreshing', [refreshing]); }, }; class PullToRefreshFooter extends Component { _nativeRef = null; _lastNativeRefreshing = false; _onRefresh = () => { this._lastNativeRefreshing = true; this.props?.onRefresh?.(); this.forceUpdate(); }; _onStateChanged = (event) => { this.props?.onStateChanged?.({ ...event, nativeEvent: { state: event.nativeEvent.state, }, }); }; _setNativeRef = (ref) => { this._nativeRef = ref; }; componentDidMount() { this._lastNativeRefreshing = this.props.refreshing; } componentDidUpdate(prevProps) { if (this.props.refreshing !== prevProps.refreshing) { this._lastNativeRefreshing = this.props.refreshing; } else if (this.props.refreshing !== this._lastNativeRefreshing && this._nativeRef) { PullToRefreshFooterCommands.setNativeRefreshing(this._nativeRef, this.props.refreshing); this._lastNativeRefreshing = this.props.refreshing; } } render() { return (<PullToRefreshFooterNativeComponent {...this.props} ref={this._setNativeRef} onRefresh={this._onRefresh} onStateChanged={this._onStateChanged}/>); } } export { PullToRefreshFooter };