At the end of last year, I tried to write a componentized page using react! There is a list page that displays the data in pages Show the three main components: parent component listBox, list component List, button component PageButton Parent component listBoxconst listData = [{ key:"001", idd:"001", title:"Webstorm connects to GitHub for easy warehouse management", time:"2016-12-01", tag:" git ", contents:"666666666666666!" }] // And so on and so forth for multiple data class listBox extends Component { constructor(props){ super(props); this.pageNext=this.pageNext.bind(this); this.setPage=this.setPage.bind(this); this.state = { indexList:[], //Currently rendered page data totalData:listData, current: 1, //Current page numberpageSize:4, //Number of items displayed per pagegoValue:0, //Number of items to goindex totalPage:0, //Total number of pages}; } componentWillMount(){ //Set the total number of pages this.setState({ totalPage:Math.ceil( this.state.totalData.length/this.state.pageSize), }) this.pageNext(this.state.goValue) } //Set the content setPage(num){ this.setState({ indexList:this.state.totalData.slice(num,num+this.state.pageSize) }) } pageNext (num) { this.setPage(num) } render() { return ( <div className="main"> <div className="top_bar"> </div> <div className="lists"> <ul className="index"> {this.state.indexList.map(function (cont) { return <List {...cont} /> })} </ul> <PageButton { ...this.state } pageNext={this.pageNext} /> </div> </div> ); } } List componentclass list extends Component { constructor(props) { super(props); } render() { const { idd, title, time, tag, contents } = this.props return ( <li id={idd}> <Link to={`/list/listmore/${idd}`} > <h3>{title}</h3> <div className="icon"> <i className="fa fa-calendar"></i> <span>Published at {time} </span> <i className="fa fa-sitemap"></i> <span>Categorized under {tag} </span> <i className="fa fa-edit"></i> <span>No comments yet</span> </div> <p>{contents}</p> <span className="more">more</span> </Link> </li> ); } } Button component PageButtonclass pageButton extends Component { constructor(props) { super(props); this.setNext=this.setNext.bind(this); this.setUp = this.setUp.bind(this); this.state={ num: 0, pagenum:this.props.current } } //Next page setNext(){ if(this.state.pagenum < this.props.totalPage){ this.setState({ num:this.state.num + this.props.pageSize, pagenum:this.state.pagenum + 1 },function () { console.log(this.state) this.props.pageNext(this.state.num) }) } } //Previous page setUp(){ if(this.state.pagenum > 1){ this.setState({ num:this.state.num - this.props.pageSize, pagenum:this.state.pagenum - 1 },function () { console.log(this.state) this.props.pageNext(this.state.num) }) } } render() { return ( <div className="change_page"> <span onClick={ this.setUp } >Previous page</span> <span>{ this.state.pagenum } page / { this.props.totalPage } page </span> <span onClick={ this.setNext }>Next page</span> </div> ); } } This is the end of this article about how to display data in paging mode in react. For more relevant content about paging mode in react, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Solve the problems encountered when installing MySQL 8.0 on Win10 system
>>: Detailed explanation of the practical record of solving network isolation through Nginx
Table of contents 1. Preparation 1. Prepare the e...
Table of contents 1. Character Function 1. Case c...
I have been making websites for a long time, but I...
Preface In the development process, defining vari...
What is serdel userdel is a low-level tool for de...
Mixins provide distributed reusable functionality...
It seems that the mysql-sever file for installing...
It is very simple to build a go environment under...
Element form and code display For details, please...
1. Install MySQL # Download mysql in docker docke...
1. Parent components can pass data to child compo...
Table of contents 1. Merge arrays 2. Merge arrays...
Table of contents 1. Rendering 2. Bind data and a...
Copy code The code is as follows: <html> &l...
This article shares the specific code of a simple...