React Element change class onClick

Bogdan Daniel

I have created a custom pagination in React: I basically have in the container pagination which will be visible only when data is received:

...
    {this.state.displayTweets.length != 0 && <Pagination pageCount={this.state.pageCount} pagination={this.pagination.bind(this)}/>}
...

This is the function that is called when a pagination item is clicked:

pagination(data){
        if(data == -1 && offset>0){
            console.log("Previous")
            offset -= perPage
            this.setState({displayTweets: this.state.tweets.slice(offset,offset+perPage)})
        } else if(data == 0 && pageNumber < this.state.pageCount) {
            console.log("Next")
            pageNumber += 1
            offset += perPage
            this.setState({displayTweets: this.state.tweets.slice(offset,offset+perPage)});
        } else if(data > 0 && data < this.state.pageCount){
            console.log("Between")
            pageNumber = data
            offset = perPage*(data-1)
            this.setState({displayTweets: this.state.tweets.slice(offset,perPage*data)});
        }
        console.log("Page number: "+pageNumber)
        console.log("Offset: "+offset)
    }

And here is the actual Pagination component:

import React from 'react';

const PageLi = (props) => {
    let pages = [];
    for (let i = 1; i <= props.pageCount; i++) {
        pages.push(<li className="page-item" onClick={() => { props.pagination(i)}} key={i}><a className="page-link">{i}</a></li>);
    }
    return (
        <nav aria-label="Page navigation example">
            <ul className="pagination">
                <li className="page-item" onClick={() => { props.pagination(-1)}}><a className="page-link">Previous</a></li>
                {pages}
                <li className="page-item"><a onClick={() => { props.pagination(0)}} className="page-link">Next</a></li>
            </ul>
        </nav>
    );
};

export default PageLi;

Everything works as expected. Now I would like that when I click on a pagination item to change the class from page-item to page-item-active and remove the active part from the previously clicked one. How can I do this in React? I know that I can set states, but to have a state for each page number would be stupid.

Tharaka Wijebandara

You can store and maintain your current page number also in your state and then pass it as a prop to PageLi component. Then you can do something as follows to change the class based on current page number.

let pages = [];
for (let i = 1; i <= props.pageCount; i++) {
  pages.push(
    <li
      className={i === props.pageNumber ? "page-item-active" : "page-item"}
      onClick={() => {
        props.pagination(i);
      }}
      key={i}
    >
      <a className="page-link">{i}</a>
    </li>
  );
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

jQuery onclick to change class name of an element

From Dev

Change children class with parent onClick in React?

From Dev

ReactJS: onClick change element

From Dev

What is the best way to change the URL onClick or onSubmit in a React Class Component?

From Dev

Css onclick change class

From Dev

onclick after class change

From Dev

React class prop onclick

From Dev

Change element style CSS onclick?

From Dev

Change div id/class onclick

From Dev

change class of element onkeyup

From Dev

How to change display onclick in React?

From Dev

Change classes to an element with react

From Dev

OnClick change color from another element

From Dev

javascript change element background onclick calling function

From Dev

OnClick change color from another element

From Dev

icon change onclick of menu element jquery

From Dev

Selecting only one element in the class onclick

From Dev

Selecting only one element in the class onclick

From Dev

changing display in an element of class by onclick method in javascript

From Dev

change class of current button element

From Dev

Change class of iframe element on click

From Dev

Change class of an element using AngularJS

From Dev

Change element class while dragging

From Dev

change the class of an element without ID

From Dev

If element has class and another element is empty change class on another element

From Dev

how to change class element in enumeration element in EA

From Dev

Sequential DIV fadeIn/Out onClick - class change

From Dev

jQuery onclick event for <a> tag to change 'active' class

From Dev

Render new element onClick in react.js