Now I would like to pull down a scrollView and refresh this component, so I have followed the official document mentioning onRefresh and RefreshContorol from react-native.
However, I don't know why my code is not working and get error...
The code below is my code.
<View style={styles.container}>
<ScrollView
contentContainerStyle={{ flexDirection: 'row', flexWrap: 'wrap' }}
refreshControl={<RefreshControl refreshing={this.state.refreshing} onRefresh={this.setState({ refreshing: true })} />}
>
{this.renderItemBox()}
</ScrollView>
</View>
React Native RefreshControl If you're rendering a list of data using the ScrollView or FlatList component, you can use RefreshControl to add the pull-to-refresh capability. When you initiate the pull-to-refresh gesture, RefreshControl triggers an onRefresh event.
Fast Refresh is enabled by default, and you can toggle "Enable Fast Refresh" in the React Native developer menu. With Fast Refresh enabled, most edits should be visible within a second or two.
React Native ListView is a view component which contains the list of items and displays in a vertical scrollable list. The minimum API to create list view is ListView. DataSource. It populates a simple array of data blobs, and instantiate a ListView component with data source and a renderRow callback.
Below is the sample code in which you can find RefreshController integration with ScrollView:
import { ScrollView, RefreshControl } from 'react-native';
class RefreshableList extends Component {
constructor(props) {
super(props);
this.state = {
refreshing: false,
};
}
_onRefresh = () => {
this.setState({refreshing: true});
fetchData().then(() => {
this.setState({refreshing: false});
});
}
render() {
return (
<ScrollView
refreshControl={
<RefreshControl
refreshing={this.state.refreshing}
onRefresh={this._onRefresh}
/>
}
/>
);
}
}
on me it worked
import React from 'react';
import {
RefreshControl,
ScrollView,
StyleSheet,
View,
} from 'react-native';
import {WebView} from 'react-native-webview';
const wait = timeout => {
return new Promise(resolve => setTimeout(resolve, timeout));
};
const Main = () => {
const webViewRef = React.useRef();
const [refreshing, setRefreshing] = React.useState(false);
const [enablePTR, setEnablePTR] = React.useState(false);
const onRefresh = React.useCallback(() => {
webViewRef.current.reload();
setRefreshing(true);
wait(2000).then(() => setRefreshing(false));
}, []);
const handleEvent = e => {
if (e.nativeEvent.contentOffset.y > 100) {
setEnablePTR(false);
} else {
setEnablePTR(true);
}
};
return (
<View style={styles.container}>
<ScrollView
scrollEnabled={false}
contentContainerStyle={styles.container}
refreshControl={
<RefreshControl
refreshing={refreshing}
onRefresh={onRefresh}
enabled={enablePTR}
/>
}
>
<WebView
source={{ uri: "https://example.com/" }}
ref={(ref) => (webViewRef.current = ref)}
javaScriptEnabled={true}
injectedJavaScript="setTimeout(() => {document.addEventListener('scroll', function (event) {window.ReactNativeWebView.postMessage(JSON.stringify(document.getElementsByClassName('topconteiner')[0].scrollTop));},true);}, 300);true;"
onScroll={(event) => handleEvent(event)}
/>
</ScrollView>
</View>
);
};
export default Main;
const styles = StyleSheet.create({
container: {backgroundColor: '#fff', flex: 1},
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With