Add ScrollToTop component

This commit is contained in:
SachaG 2019-01-18 14:05:53 +09:00
parent e0433ca74f
commit 1b36fe2ea1
3 changed files with 21 additions and 1 deletions

View file

@ -17,7 +17,7 @@ import withSiteData from '../containers/withSiteData.js';
import { withApollo } from 'react-apollo';
import { withCookies } from 'react-cookie';
import moment from 'moment';
import { Switch, Route, BrowserRouter } from 'react-router-dom';
import { Switch, Route } from 'react-router-dom';
import { withRouter} from 'react-router';
const DummyErrorCatcher = ({ children }) => children;
@ -142,6 +142,7 @@ class App extends PureComponent {
return (
<IntlProvider locale={this.getLocale()} key={this.getLocale()} messages={Strings[this.getLocale()]}>
<Components.ScrollToTop />
<div className={`locale-${this.getLocale()}`}>
<Components.HeadTags />
{/* <Components.RouterHook currentRoute={currentRoute} /> */}

View file

@ -0,0 +1,18 @@
import React, {Component} from 'react';
import {withRouter} from 'react-router';
import {registerComponent} from 'meteor/vulcan:lib';
// Scroll restoration based on https://reacttraining.com/react-router/web/guides/scroll-restoration.
export default class ScrollToTop extends Component {
componentDidUpdate(prevProps) {
if (this.props.location !== prevProps.location) {
window.scrollTo(0, 0);
}
}
render() {
return null;
}
}
registerComponent('ScrollToTop', ScrollToTop, withRouter);

View file

@ -24,6 +24,7 @@ export { default as Flash } from './components/Flash.jsx';
export { default as HelloWorld } from './components/HelloWorld.jsx';
export { default as Welcome } from './components/Welcome.jsx';
export { default as RouterHook } from './components/RouterHook.jsx';
export { default as ScrollToTop } from './components/ScrollToTop.jsx';
export { default as withAccess } from './containers/withAccess.js';
export { default as withMessages } from './containers/withMessages.js';