关于React-Router4.0跳转不置顶解决方案

在使用react-router时会遇到奇怪的问题,好比当咱们从首页进入详情页的时候,首页跳转到详情页,首页滚动的位置,进入到详情页的时候也会被记录下来,缘由是因为共享了同一个history,因此对记录有所保留,这显然不符合咱们的浏览习惯。react

总结种解决方案:react-router

方案一this

<Router onUpdate={() => window.scrollTo(0, 0)} history={hashHistory}>code

<Route path="/" component={ App }>

</Router> component

方案二router

class Protol extends React.Component {hash

constructor(props) {
    super(props);
}
 componentDidUpdate(prevProps) {
     if (this.props.location !== prevProps.location) {
         window.scrollTo(0, 0)
     }
 }
render() {
    return (
        <div>
            <Header/>

            {this.props.children}
            <Footer/>
        </div>
    );
}

}io

相关文章
相关标签/搜索