react page not starting from top code example
Example 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);
Example 2: how to make page scroll to the top jsx
componentDidMount() {
window.scrollTo(0, 0)
}