🐛 Fix scroll to initial anchor on page load and scroll to top on router change.

chore: Use destroy replace disable.
This commit is contained in:
gxvv 2019-05-16 13:28:23 +08:00
parent 5b8867439a
commit 8539cf6879

View File

@ -68,6 +68,7 @@ export default class MainContent extends Component {
componentDidMount() {
this.componentDidUpdate();
window.addEventListener('load', this.handleInitialHashOnLoad);
}
static getDerivedStateFromProps(props, state) {
@ -87,13 +88,21 @@ export default class MainContent extends Component {
this.bindScroller();
}
if (!window.location.hash && prevLocation.pathname !== location.pathname) {
document.documentElement.scrollTop = 0;
window.scrollTo(0, 0);
}
// when subMenu not equal
if (get(this.props, 'route.path') !== get(prevProps, 'route.path')) {
// reset menu OpenKeys
this.handleMenuOpenChange();
}
}
componentWillUnmount() {
this.scroller.destroy();
window.removeEventListener('load', this.handleInitialHashOnLoad);
}
handleInitialHashOnLoad() {
setTimeout(() => {
if (!window.location.hash) {
return;
@ -107,10 +116,6 @@ export default class MainContent extends Component {
}, 0);
}
componentWillUnmount() {
this.scroller.disable();
}
getMenuItems(footerNavIcons = {}) {
const { themeConfig } = this.props;
const {
@ -165,7 +170,7 @@ export default class MainContent extends Component {
bindScroller() {
if (this.scroller) {
this.scroller.disable();
this.scroller.destroy();
}
require('intersection-observer'); // eslint-disable-line
const scrollama = require('scrollama'); // eslint-disable-line