Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

disabling button on specific condition on r

I am using below code for pagination.

  <ul className="pagination">
          <li className="page-item">
            <button  className="page-link btn btn-success btn-block"  onClick={prev} >Previous</button>
          </li>
          <li className="page-item">
            <button className="page-link btn btn-success btn-block " onClick={next}>Next</button>
          </li>
        </ul>


 const next = () => {
    if (page <= lastPage) {
      setPage(page + 1);
    }
  }
  const prev = () => {
    if (page > 1) {
      setPage(page - 1);
    }
  }

my requirement is when data of first page is getting displayed then prev button should be disabled. when data of lastpage is getting displayed then nextbutton should be disabled.

I mean if(page==1) disableprev and if(page==lastPage)disable next .how can I do it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can conditionally disable a button using an expression on the disable property of the button. For example, for the Previous Button, you can do this,

<button disabled={page == 1} className="page-link btn btn-success btn-block"  onClick={prev} >Previous</button>

You can follow the same thing for the Next button too. Just change the expression to page == lastPage

But make sure that the page property is a state of the component so that React automatically refreshes the component when it changes. Regarding your use of setPage I suppose it’s already the case

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading