Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Native FlatList render a zero state when data is empty

Tags:

react-native

Is it possible to render an alternate component when the data is empty? The only reason I would not just render the list or not render the list is that the ListHeaderComponent is necessary in both scenarios (data.length and !data.length)...

const data = []
<FlatList
  contentContainerStyle={styles.list}
  data={data} // if empty or !data.length render <ZeroComponent/>
like image 966
studiobrain Avatar asked Dec 08 '17 17:12

studiobrain


Video Answer


2 Answers

UPDATE react-native recently added ListEmptyComponent

const data = []
_listEmptyComponent = () => {
    return (
        <View>
            // any activity indicator or error component
        </View>
    )
}

<FlatList
    data={data}
    ListEmptyComponent={this._listEmptyComponent}
    contentContainerStyle={styles.list} 
/>
const data = []

renderFooter = () => {
  if (data.length != 0) return null;


  return (
    <View>
      // any activity indicator or error component
    </View>
   );
};

<FlatList
    contentContainerStyle={styles.list}
    data={data}
    ListFooterComponent={this.renderFooter}
/>

Hope this helps

like image 179
Subramanya Chakravarthy Avatar answered Sep 24 '22 18:09

Subramanya Chakravarthy


There is a prop ListEmptyComponent which can do the job.

<FlatList
    data={this.state.data}
    renderItem={this.renderItem}
    keyExtractor={(item, index) => item.id}
    ListHeaderComponent={this.showFilters()}
    ListEmptyComponent={this.showEmptyListView()}
/>
like image 24
Jigish Chawda Avatar answered Sep 23 '22 18:09

Jigish Chawda