I'm learning React recently, and I want to know how to paginate in React. I have implemented a simple REST API that can fetch data from the database and return JSON file like this:
count: total number of data
start: starting index of data
limit: how many data per page
next: api to fetch next page of data
previous: api to fetch previous page of data
{
"count": 17,
"start": 1
"limit": 5,
"next": "/User/page?start=6&limit=5",
"previous": "",
"results": [
{
"email": "",
"first_name": "",
"id": ,
"last_name": "",
"password": "",
},
...
],
}
I want so I want to add two links at the bottom of each page, so that i can go forward and backwards.
My current React code looks like this, I can fetch the data from first page, but I'm not sure how to get to the next page
class Users extends Component{
state={
count:undefined,
limit:undefined,
next:'',
previous:'',
start:undefined,
results:[]
}
componentDidMount(){
Axios.get('http://127.0.0.1:5000/User/page').then(response=>{
this.setState({
results:response.data.results,
count:response.data.count,
limit:response.data.limit,
next:response.data.next,
previous:response.data.previous,
start:response.data.start
})
})
}
render(){
console.log(this.state.previous)
console.log(this.state.next)
return (
<div>
{this.state.results.map((user)=>{return <User key={user.id} user={user}/>})}
//I know this wont work
//<a href={this.state.previous} style={{color:'#4c4c4c'}}>prev</a>
//<br/>
//<a href={this.state.next} style={{color:'#4c4c4c'}}>next</a>
</div>
)
}
}