简体   繁体   中英

How to switch className between different buttons

I want to switch class of buttons when clicked to change its color accordingly. Color of particular button should change on click in react js

I have tried

{"classname" isActive: "included classname"}

here is my code...

const handlePaging=()=> {

    let totalPages = listProduct.length / entriesPerPage;
    let pageNumber = [];

    for (let i = 0; i < totalPages; i++) {

        pageNumber[i] = <li><a href={()=> false} onClick={() => handlePageNumber(i, entriesPerPage)} className={"pagination-link"}>{i+1}</a></li>
    }

    return (pageNumber)
}

const handlePageNumber=(entryNumber, entriesPerPage)=> {

    setEntryStart(entryNumber * entriesPerPage);
    setEntryEnd(((entryNumber+1)*entriesPerPage) < listProduct.length ? ((entryNumber+1)*entriesPerPage) : listProduct.length );
}

I want to add classname is-current to the button which is clicked, I am managing the pagination actually.

You will need some information about which page is selected.

// you need somewhere stored the active page.
let selectedPage = 1;

let totalPages = listProduct.length / entriesPerPage;
let pageNumber = [];

for (let i = 0; i < totalPages; i++) {
    pageNumber[i] = <li>
      <a href={()=> false} 
          onClick={() => handlePageNumber(i, entriesPerPage)} 
          className={"pagination-link " + (i === selectedPage ? "is-current" : "")}>{i+1}</a>
    </li>
}

return (pageNumber)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM