react-router4.0路由切换后使页面在顶部。

如同锚点链接那样子,跳转路由后页面在顶部

阅读 6.1k
2 个回答

react-router4.0把路由当成了组件去用。你可以在顶部组件的生命周期中去写判断。
例如这样。

 componentWillReceiveProps(nextProps){

        //当路由切换时
        if(this.props.location !== nextProps.location){
            window.scrollTo(0,0)
        }
    }
componentDidMount() {
  this.node.scrollIntoView();
}

render() {
  return (
    <div ref={node => this.node = node} > </div>
  );
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题