Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native - Pagination of ListViews in ScrollView?

I have 3 ListView components inside a single ScrollView component like this:

<ScrollView>
  <Header />
  <ListView onEndReached={() => alert('load more 1')}/>
  <ListView onEndReached={() => alert('load more 2')}/>
  <ListView onEndReached={() => alert('load more 3')}/>
  <Footer />
</ScrollView>

The Header component has some common content and also has 3 tabs, which trigger showing the respective ListView

The issue is any ListView with onEndReached={() => alert('load more 1')} never runs the alert, so I can never load more as I scroll down and hit the end of the listview. Remove the wrapping ScrollView and the alert runs, but the common Header doesn't scroll, since we just removed the wrapping ScrollView. The header needs to scroll with the listview, which is why I wrapped everything that needs to scroll in the ScrollView.

IMPORTANT NOTE: I can't really use ListView with renderHeader={this.header}, for this scenario. Because, even though the Header will scroll, it will rerender the common Header and the 3 tabs for each ListView each time a ListView renders, instead of once. So a new Header rerender each time for each ListView won't cut it for the app.

Looking for a solution to this problem, where the Header scrolls with the listviews and the onEndReached is triggered for the visible ListView.

like image 234
Wonka Avatar asked Aug 11 '16 04:08

Wonka


People also ask

How do you scroll to particular element in React Native?

React 16.3 +, Class component myRef}>Element to scroll to</div> } executeScroll = () => this. myRef. current. scrollIntoView() // run this method to execute scrolling. }

How do you scroll horizontally React Native?

In the ScrollView, we can scroll the components in both direction vertically and horizontally. By default, the ScrollView container scrolls its components and views in vertical. To scroll its components in horizontal, it uses the props horizontal: true (default, horizontal: false).


1 Answers

I think you're going to have to solve this by changing the dataSource in each listView in response to what header element is selected instead of loading three different ListViews.

getInitialState() {
    return {
        currentList: this.ds.cloneWithRowsAndSections(INITIAL_DATA);
    }
},
render() {
    return <ListView renderHeader={this._renderHeader} dataSource={this.state.currentList}/>
}

The only reason you wouldn't want to do this is if you wanted to maintain the scroll position in the three sub ListViews, but that wouldn't be useful because you always have to scroll to the top to change which ListView you're looking at anyway.

Then in your _renderHeader function you would render a selector that populates currentList with different data depending on the header selected.

like image 73
frank Avatar answered Oct 15 '22 06:10

frank