From ec6b11d9c9a7bb6861486a4b19522ecd4f10040d Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 5 Aug 2018 11:20:09 +0200 Subject: [PATCH] Created component to scroll to top --- src/common/ScrollToTop.js | 22 ++++++++++++++++++++++ src/index.js | 5 ++++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/common/ScrollToTop.js 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')