Here is an example of React Native Swipe Down to Refresh List View Using Refresh Control. It was first introduced in Android Material Design and became very popular. Almost all Apps are using Swipe down to refresh. In React Native you can use this feature using RefreshControl
provided by React Native.
To Import Refresh Control in Code
1 | import { RefreshControl} from 'react-native' |
Swipe Down to Refresh List View Using Refresh Control
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <FlatList data={this.state.dataSource} ItemSeparatorComponent={this.ListViewItemSeparator} enableEmptySections={true} keyExtractor={(item, index) => index.toString()} renderItem={({item}) => ( <Text style={styles.rowViewContainer} onPress={() => alert(item.id)}> {item.title} </Text> )} refreshControl={ <RefreshControl //refresh control used for the Pull to Refresh refreshing={this.state.refreshing} onRefresh={this.onRefresh.bind(this)} /> } /> |
In this example, we will make a simple list which is having from the network call and we can refresh the data using swiping down. So let’s get started.
Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the Run the following commands to create a new React Native project If you want to start a new project with a specific React Native version, you can use the --version argument: This will make a project structure with an index file named App.js in your project directory.To Make a React Native App
react-native-cli
command line utility. Open the terminal and go to the workspace and runnpm install -g react-native-cli
react-native init ProjectName
react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next
Open App.js in any code editor and replace the code with the following code.
App.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | //This is an example code for React Native Swipe Down to Refresh ListView Using RefreshControl// import React, { Component } from 'react'; //import react in our code. import { StyleSheet, ActivityIndicator, FlatList, Text, View, Alert, RefreshControl, } from 'react-native'; //import all the components we are going to use. export default class Project extends Component { constructor(props) { super(props); //True to show the loader this.state = { refreshing: true }; //Running the getData Service for the first time this.GetData(); } GetData = () => { //Service to get the data from the server to render return fetch('https://jsonplaceholder.typicode.com/posts') .then(response => response.json()) .then(responseJson => { this.setState({ refreshing: false, //Setting the data source for the list to render dataSource: responseJson }); }) .catch(error => { console.error(error); }); }; ListViewItemSeparator = () => { return ( //returning the listview item saparator view <View style={{ height: 0.2, width: '90%', backgroundColor: '#808080', }} /> ); }; onRefresh() { //Clear old data of the list this.setState({ dataSource: [] }); //Call the Service to get the latest data this.GetData(); } render() { if (this.state.refreshing) { return ( //loading view while data is loading <View style={{ flex: 1, paddingTop: 20 }}> <ActivityIndicator /> </View> ); } return ( //Returning the ListView <View style={styles.MainContainer}> <FlatList data={this.state.dataSource} keyExtractor={(item, index) => index.toString()} ItemSeparatorComponent={this.ListViewItemSeparator} enableEmptySections={true} renderItem={({item}) => ( <Text style={styles.rowViewContainer} onPress={() => alert(item.id)}> {item.title} </Text> )} refreshControl={ <RefreshControl //refresh control used for the Pull to Refresh refreshing={this.state.refreshing} onRefresh={this.onRefresh.bind(this)} /> } /> </View> ); } } const styles = StyleSheet.create({ MainContainer: { justifyContent: 'center', flex: 1, marginTop: 10, }, rowViewContainer: { fontSize: 20, padding: 10, }, }); |
To Run the React Native App
Open the terminal again and jump into your project using.cd ProjectName
To run the project on an Android Virtual Device or on real debugging devicereact-native run-android
or on the iOS Simulator by runningreact-native run-ios
(macOS only).
That was the React Native React Native Swipe Down to Refresh ListView Using RefreshControl. If you have any doubt or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!
Hope you liked it. 🙂