Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to re-render flatlist?

Unlike ListView we can update this.state.datasource. Is there any method or example to update FlatList or re-render it?

My goal is to update the text value when user press button ...

renderEntries({ item, index }) {     return(         <TouchableHighlight onPress={()=> this.setState({value: this.state.data[index].value+1})>              <Text>{this.state.data[index].value}</Text>         </TouchableHighlight>     ) }  <FlatList      ref={(ref) => { this.list = ref; }}      keyExtractor={(item) => item.entry.entryId}      data={this.state.data}      renderItem={this.renderEntries.bind(this)}      horizontal={false} /> 
like image 386
shalonteoh Avatar asked Apr 13 '17 16:04

shalonteoh


People also ask

How do you refresh a 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 you're-render FlatList in React Native functional component?

You can force flatlist to rerender by passing the updated list as an extraData prop, i.e extraData={listData} . However, when using functional components a common mistake is passing the same instance of the list data as the prop.

How do you auto refresh FlatList in React Native?

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.

What is extraData in FlatList?

By passing extraData={selectedId} to FlatList we make sure FlatList itself will re-render when the state changes. Without setting this prop, FlatList would not know it needs to re-render any items because it is a PureComponent and the prop comparison will not show any changes.


2 Answers

Use the extraData property on your FlatList component.

As the documentation states:

By passing extraData={this.state} to FlatList we make sure FlatList will re-render itself when the state.selected changes. Without setting this prop, FlatList would not know it needs to re-render any items because it is also a PureComponent and the prop comparison will not show any changes.

like image 164
ED209 Avatar answered Sep 22 '22 10:09

ED209


For quick and simple solution Try:

  1. set extra data to a boolean value.

    extraData={this.state.refresh}

  2. Toggle the value of boolean state when you want to re-render/refresh list

    this.setState({      refresh: !this.state.refresh }) 
like image 20
Hradesh Kumar Avatar answered Sep 23 '22 10:09

Hradesh Kumar