React method of displaying data in pages

React method of displaying data in pages

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 listBox

const 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 component

class 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 PageButton

class 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:
  • React implements paging effect
  • Try to write a paging component with react yourself (summary)
  • Reactjs implements the example code of the universal paging component

<<:  Solve the problems encountered when installing MySQL 8.0 on Win10 system

>>:  Detailed explanation of the practical record of solving network isolation through Nginx

Recommend

Detailed process of using vmware to test PXE batch installation server

Table of contents 1. Preparation 1. Prepare the e...

An example of the difference between the id and name attributes in input

I have been making websites for a long time, but I...

How to define data examples in Vue

Preface In the development process, defining vari...

Detailed tutorial on how to delete Linux users using userdel command

What is serdel userdel is a low-level tool for de...

Detailed explanation of mixins in Vue.js

Mixins provide distributed reusable functionality...

Tutorial on installing MySQL on Alibaba Cloud Centos 7.5

It seems that the mysql-sever file for installing...

Notes on element's form components

Element form and code display For details, please...

Detailed process of installing various software in Docker under Windows

1. Install MySQL # Download mysql in docker docke...

A brief discussion on VUE uni-app custom components

1. Parent components can pass data to child compo...

JavaScript Shorthand Tips

Table of contents 1. Merge arrays 2. Merge arrays...

Vue Element-ui implements tree control node adding icon detailed explanation

Table of contents 1. Rendering 2. Bind data and a...

Use js to write a simple snake game

This article shares the specific code of a simple...