Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement pull to refresh FlatList

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); 
like image 322
petros Avatar asked Nov 30 '17 10:11

petros


People also ask

How do you implement pull to refresh in React Native FlatList?

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.

How do I auto refresh in react?

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!

How can I make my FlatList faster?

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.


2 Answers

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 }) 
like image 143
krish Avatar answered Sep 23 '22 05:09

krish


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}     />   } />      
like image 38
Jigar Avatar answered Sep 20 '22 05:09

Jigar