Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native FlatList inside ScrollView onEndReached never called

My container is a scrollview and inside it is a flatlist, which load data from server.

The flatlist:

<VirtualizedList
  ListEmptyComponent={<NoData />}
  data={data}
  getItem={(items, index) => items.get(index)}
  getItemCount={(items) => items.size}
  keyExtractor={(item, index) => String(index)}
  renderItem={this._renderItem}
  refreshControl={
     <RefreshControl
       refreshing={loading}
       onRefresh={this._onRefresh.bind(this)}
     />
  }
  onEndReached={this.handleLoadMore}
  onEndReachedThreshold={0.1}
  onMomentumScrollBegin={() => {
    Log('onMomentumScrollBegin fired!')
    this.onEndReachedCalledDuringMomentum = false;
  }}
/>

which handleLoadMore is:

handleLoadMore = () => {
  Log('handleLoadMore fired!');
  if (!this.onEndReachedCalledDuringMomentum) {
    // fetch data..
    this.onEndReachedCalledDuringMomentum = true;
  }
}

The problem is the handleLoadMore never called and the onMomentumScrollBegin also never called.

How to solve this?

like image 391
FaiChou Avatar asked Jul 13 '18 07:07

FaiChou


People also ask

Can we use FlatList inside ScrollView react native?

"VirtualizedLists should never be nested inside plain ScrollViews with the same orientation" error in console for FlatList/SectionList with scrollEnabled={false}

Can we use ScrollView inside FlatList?

Under the hood, FlatList uses the ScrollView component to render scrollable elements. However, unlike generic ScrollView, FlatList displays data lazily to save memory and processing time.

What is onEndReachedThreshold?

onEndReachedThreshold ​How far from the end (in units of visible length of the list) the bottom edge of the list must be from the end of the content to trigger the onEndReached callback. Thus a value of 0.5 will trigger onEndReached when the end of the content is within half the visible length of the list.


1 Answers

Actually you don't need to use Content or ScrollView as FlatList has both ListFooterComponent and ListHeaderComponent

In case you really need to use FlatList inside ScrollView, then add style and content contentContainerStyle to your ScrollView or if you use native-base, inside the Content

<ScrollView
  ...
  style={{flex: 1}}
  contentContainerStyle={{flex: 1}}
  ...
/>

Then if you want to use header component out of FlatList loop. Then use ListHeaderComponent={() => <SomeHeader /> inside Flatlist.

like image 177
Nagibaba Avatar answered Oct 22 '22 16:10

Nagibaba