Answers for "make sure a page loads at the top react"

6

scroll to top in react

componentDidMount() {
  window.scrollTo(0, 0)
}
Posted by: Guest on August-28-2020
1

handling scrolling on react router transitions

import { useEffect } from 'react';
import { withRouter } from 'react-router-dom';

function ScrollToTop({ history }) {
  useEffect(() => {
    const unlisten = history.listen(() => {
      window.scrollTo(0, 0);
    });
    return () => {
      unlisten();
    }
  }, []);

  return (null);
}

export default withRouter(ScrollToTop);
Posted by: Guest on November-06-2020

Code answers related to "make sure a page loads at the top react"

Code answers related to "Javascript"

Browse Popular Code Answers by Language