javascript - Active class in pagination in React - Stack Overflow

I've made a pagination, everything works, but I don't know how to set active class on single

I've made a pagination, everything works, but I don't know how to set active class on single page.

This is my example:

Pagination code:

    const renderPagination = numbers.map(number => {
        return (
            <li className="controls" key={number} id={number} onClick={this.handlePages}>
                {number}
            </li>
        );
    });

Full example:

codepen

I've made a pagination, everything works, but I don't know how to set active class on single page.

This is my example:

Pagination code:

    const renderPagination = numbers.map(number => {
        return (
            <li className="controls" key={number} id={number} onClick={this.handlePages}>
                {number}
            </li>
        );
    });

Full example:

codepen

Share Improve this question edited May 31, 2018 at 14:22 Neil asked May 31, 2018 at 14:07 NeilNeil 151 silver badge4 bronze badges 3
  • Do you mean on the buttons of the page switching element? Like highlight the button "2" when on page 2? – Pierre C. Commented May 31, 2018 at 14:13
  • Yeah, that's what I mean. – Neil Commented May 31, 2018 at 14:16
  • @neil, you specifically got an error that you need to include your code in the post. Trying to circumvent that requirement by marking some random text as code isn't cool. Please insert your code to your post. – Chris Commented May 31, 2018 at 14:19
Add a ment  | 

2 Answers 2

Reset to default 6
<li className={(this.state.currentPage === number ? 'active ' : '') + 'controls'} key={number} id={number} onClick={this.handlePages}>
  {number}
</li>

You can add class when state equal number

https://codepen.io/titan_dl_1904/pen/XYbjye

Since the currentPage is in the state you can grab it from there and pare it to the number of the page link you are displaying

    const renderPagination = numbers.map(number => {
        var activeClass = this.state.currentPage === number ? 'active' : '';
        return (
            <li className={`controls ${activeClass}`} key={number} id={number} onClick={this.handlePages}>
                {number}
            </li>
        );
    });

Updated demo (with some styling too): https://codepen.io/gpetrioli/pen/MXwbYv

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744822346a4595692.html

相关推荐

  • javascript - Active class in pagination in React - Stack Overflow

    I've made a pagination, everything works, but I don't know how to set active class on single

    2天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信