UNPKG

daniel-pull-to-refresh

Version:
37 lines (36 loc) 1.44 kB
import React, { Component } from 'react'; import { findNodeHandle, requireNativeComponent, UIManager } from 'react-native'; const NativePullToRefreshHeader = requireNativeComponent('RefreshHeader'); const PullToRefreshHeaderCommands = { setNativeRefreshing(componentOrHandle, refreshing) { UIManager.dispatchViewManagerCommand(findNodeHandle(componentOrHandle), 'setNativeRefreshing', [refreshing]); }, }; class PullToRefreshHeader extends Component { _nativeRef = null; _lastNativeRefreshing = false; _onRefresh = () => { this._lastNativeRefreshing = true; this.props?.onRefresh?.(); this.forceUpdate(); }; _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) { PullToRefreshHeaderCommands.setNativeRefreshing(this._nativeRef, this.props.refreshing); this._lastNativeRefreshing = this.props.refreshing; } } render() { return <NativePullToRefreshHeader {...this.props} ref={this._setNativeRef} onRefresh={this._onRefresh}/>; } } export { PullToRefreshHeader };