diff --git a/src/common/ScrollToTop.js b/src/common/ScrollToTop.js new file mode 100644 index 00000000..b5eff867 --- /dev/null +++ b/src/common/ScrollToTop.js @@ -0,0 +1,22 @@ +import React from 'react'; +import { withRouter } from 'react-router-dom' + +export class ScrollToTop extends React.Component { + componentDidUpdate(prevProps) { + const { location, window } = this.props; + + if (location !== prevProps.location) { + window.scrollTo(0, 0) + } + } + + render() { + return this.props.children; + } +} + +ScrollToTop.defaultProps = { + window +}; + +export default withRouter(ScrollToTop); diff --git a/src/index.js b/src/index.js index 39d129d4..df2b7293 100644 --- a/src/index.js +++ b/src/index.js @@ -8,6 +8,7 @@ import ReduxThunk from 'redux-thunk'; import App from './App'; import './index.scss'; +import ScrollToTop from './common/ScrollToTop' import reducers from './reducers'; import registerServiceWorker from './registerServiceWorker'; @@ -21,7 +22,9 @@ const store = createStore(reducers, composeEnhancers( ReactDOM.render( - + + + , document.getElementById('root')