在使用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