pagination in react native

前端 未结 2 1719
迷失自我
迷失自我 2020-12-22 06:04

I am implementing pagination in react native app my Web API is giving total record size and option to send page no as a parameter. www.URL.com/api/GetAll?pageNo={pageNumber

相关标签:
2条回答
  • 2020-12-22 07:04

    You can use onEndReached prop of FlatList to know when the list ended and then you can get the next page content then push the received data to your existing data. You can set when the endReached event will fire with onEndReachedThreshold prop.

    0 讨论(0)
  • 2020-12-22 07:08

    You can use below code. Please note that ListService.getList is a function which calls the api end point with relevant page number.

    class List extends Component {
    
        constructor() {
            super();
            this.state = {
                list: [],
                pageNumber: 1,
            };
            this.fetchList = this.fetchList.bind(this);
            this.onEndReached = this.onEndReached.bind(this);
        }
    
        fetchList() {
            ListService.getList(this.state.pageNumber)
                .then((response) => {
                    this.setState({ list: response.data, pageNumber: this.state.pageNumber + 1 });
                })
                .catch((error) => { });
        }
    
        onEndReached() {
            this.fetchList();
        }
    
        render() {
            return (
                <FlatList
                    keyExtractor={(item, index) => item.ID}
                    data={this.state.list}
                    onEndReached={this.onEndReached}
                    onEndReachedThreshold={0.3} />
            )
        }
    };
    
    0 讨论(0)
提交回复
热议问题