简体   繁体   中英

How to get active class for pagination when it is on the first page?

I am troubled to get active class when not paging, I mean when the user on the first page? The concept is when user come on the posts page then default active pagination 1, here below is my code

let pageNum = 'undefined' // it goes 2, 3, 4 when pagination clicked
let totalPages = 4

const pager = () => {
    const paginate = []

    for(let i = 1; i <= totalPages; i++){
        paginate.push(
            <Link href={`?page=${i}`} key={i}>
                <a 
                    className={
                        `page-numbers ${(parseInt(pageNum) === i) ? 'current' : null}`
                    }
                >
                    {i}
                </a>
            </Link>
        )
    }
    return paginate
}


// render
{pager()}

The above code is working well even get active pagination number when pagination clicked but not in default active, how do I do that?

Note: please suggest me if I have done wrong in this question like a formate update or anything confusion.

Thanks in advance.

change let pageNum = 'undefined' to let pageNum = 1 . If you do this you can also remove the parseInt function.

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