I have a multipage application. I need to implement scroll to top automatically when I traverse to a new page. I have tried following:
ScrollToTop.js this is placed inside component folder
import React, { Component } from 'react';
import { withRouter } from 'react-router';
class ScrollToTop extends Component {
componentDidUpdate(prevProps) {
if (this.props.location !== prevProps.location) {
window.scrollTo(0, 0)
}
}
render() {
return this.props.children
}
}
export default withRouter(ScrollToTop)
And App.js
import {Router} from 'react-router-dom'
import ScrollToTop from './common/components/ScrollToTop'
const Appnew = () => (
<Router>
<ScrollToTop>
<Appnew/>
</ScrollToTop>
</Router>
)
const App = (props) => {
{Appnew()}
}
This is not working. Any suggestions?