UNPKG

@sdcx/pull-to-refresh

Version:
46 lines (45 loc) 1.8 kB
import React, { Component } from 'react'; import { findNodeHandle, UIManager } from 'react-native'; import PullToRefreshHeaderNativeComponent from './PullToRefreshHeaderNativeComponent'; 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(); }; _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) { console.warn('setNativeRefreshing', this.props.refreshing); PullToRefreshHeaderCommands.setNativeRefreshing(this._nativeRef, this.props.refreshing); this._lastNativeRefreshing = this.props.refreshing; } } render() { return (<PullToRefreshHeaderNativeComponent {...this.props} ref={this._setNativeRef} onRefresh={this._onRefresh} onStateChanged={this._onStateChanged} progressViewOffset={this.props.progressViewOffset}/>); } } export { PullToRefreshHeader };