please help me out to implement pull to refresh on my app, I'm kinda new to react native, thanks. I don't know how to handle onRefresh and refreshing.
class HomeScreen extends Component { state = { refreshing: false } _renderItem = ({ item }) => <ImageGrid item={item} /> _handleRefresh = () => { }; render() { const { data } = this.props; if (data.loading) { return ( <Root> <Loading size="large" /> </Root> ) } return ( <Root> <HomeHeader /> <FlatList contentContainerStyle={{ alignSelf: 'stretch' }} data={data.getPosts} keyExtractor={item => item._id} renderItem={this._renderItem} numColumns={3} refreshing={this.state.refreshing} onRefresh={this._handleRefresh} /> </Root> ); } } export default graphql(GET_POSTS_QUERY)(HomeScreen);
To implement pull to refresh FlatList with React Native, we can set the refreshing and onRefresh props. to set the refreshing prop to isFetching , which is true when we're getting data for the FlatList . And we set onRefresh to the onRefresh which sets refreshing to true and then set it back to false after 2 seconds.
If set to true, the browser will do a complete page refresh from the server and not from the cached version of the page. import React from 'react'; function App() { function refreshPage() { window. location. reload(false); } return ( <div> <button onClick={refreshPage}>Click to reload!
Use basic components The more complex your components are, the slower they will render. Try to avoid a lot of logic and nesting in your list items. If you are reusing this list item component a lot in your app, create a component only for your big lists and make them with as little logic and nesting as possible.
Set variable
this.state = { isFetching: false, }
Create Refresh function
onRefresh() { this.setState({isFetching: true,},() => {this.getApiData();}); }
And in last set onRefresh and refreshing in FlatList.
<FlatList data={ this.state.FlatListItems } onRefresh={() => this.onRefresh()} refreshing={this.state.isFetching} />
After fetching Data in function getApiData Make sure to set false isFetching.
this.setState({ isFetching: false })
You can also use refreshControl
in Flatlist
, ScrollView
, and any other list component.
<FlatList contentContainerStyle={{ alignSelf: 'stretch' }} data={data.getPosts} keyExtractor={(item) => item._id} renderItem={this._renderItem} numColumns={3} refreshControl={ <RefreshControl refreshing={this.state.refreshing} onRefresh={this._handleRefresh} /> } />
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