Catherine22 / react-native-endless-scrolling-flatlist

Scroll to load more items

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

react-native-endless-scrolling-flatlist

Scrolling to load 10 more items with FlatList.

video

code

In ImageWall,

  1. Set how many items we are going to load
const DIFF = 10;
  1. Initialise the state
constructor(props) {
    super(props);
    this.state = {
        dataSource: [],
        visibleCells: [],
        loadedDataFrom: 0,
        loadedDataTo: 0
    };
}
  1. Set the FlatList
<FlatList
    ListFooterComponent={this._footer}
    ListHeaderComponent={this._header}
    ListEmptyComponent={this._empty}
    renderItem={this._renderItem}
    refreshing={false}
    onEndReachedThreshold={0.5}
    maxToRenderPerBatch={DIFF}
    windowSize={2 * DIFF}
    onEndReached={() => this._loadMore()}
    data={this.state.visibleCells}
    keyExtractor={(item) => item.key}
/>
  1. Load more
_loadMore = (dataSource = this.state.dataSource) => {
    console.log(TAG, 'Load more');
    const { loadedDataFrom, loadedDataTo, visibleCells } = this.state;
    if (dataSource === undefined || dataSource.length === 0) {
        console.log(TAG, 'No data');
        return;
    }

    if (loadedDataTo >= dataSource.length) {
        console.log(TAG, 'Reached bottom');
    }

    const footer = (loadedDataTo + DIFF <= dataSource.length) ? loadedDataTo + DIFF : dataSource.length;
    const newVisibleCells = [];

    // accelerate the calculation but fail to refresh loaded cells
    // newVisibleCells = newVisibleCells.concat(visibleCells);
    // for (let i = loadedDataTo; i < footer; i++) {
    //     newVisibleCells.push(dataSource[i]);
    // }

    for (let i = 0; i < footer; i++) {
        newVisibleCells.push(dataSource[i]);
    }

    this.setState({
        dataSource,
        visibleCells: newVisibleCells,
        loadedDataFrom: 0,
        loadedDataTo: footer
    });
};

Notice: ListFooterComponent, ListHeaderComponent and ListEmptyComponent are optional.

License

Copyright 2018 Catherine Chen (https://github.com/Catherine22)

Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.

About

Scroll to load more items

License:Apache License 2.0


Languages

Language:JavaScript 77.4%Language:Objective-C 12.7%Language:Python 5.4%Language:Java 4.4%