Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll at bottom in react on button

I am trying to scroll down user at bottom when user click on button . I really tried hard but didn't find any solution . Could someone please help me how to achieve my goal .

Thanks

like image 810
Jonas Avatar asked Oct 19 '25 02:10

Jonas


1 Answers

You can use document.body.offsetHeight to get the height of the page and scroll to it using windows.scroll. If you need to support IE11 and lower use window.scroll(0, document.body.offsetHeight);

import React from 'react';

function App() {

  function handleScroll() {
    window.scroll({
      top: document.body.offsetHeight,
      left: 0, 
      behavior: 'smooth',
    });
  }

  return <button type="button" onClick={handleScroll}>Scroll</button>;

}
like image 183
Emre Koc Avatar answered Oct 21 '25 17:10

Emre Koc