Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React router v4 onUpdate

I have recently updated to react router version 4. In the previous version I was using the onUpdate call back to trigger a function to make the page scroll to the top when the route has been changed.

It appears that onUpdate has been deprecated and I cannot find anywhere in the docs what it has been replaced with.

Has anyone else come across this issue?

const handlePageChange = () => {
    window.scrollTo(0, 0);
};  

ReactDOM.render(
    <Provider store={store}>
        <Router onUpdate={handlePageChange} history={browserHistory}>
            <Redirect from="/" to="/music" />
            {routes}
        </Router>
    </Provider>,
    document.getElementById('root')
);
like image 432
peter flanagan Avatar asked May 22 '17 08:05

peter flanagan


1 Answers

"onUpdate" is depreciated. You can use "onEnter" property in "Routes".

<Router history={browserHistory}>
  <Route path="/" component={App} >
    <IndexRoute component={Home} />
    <Route path="/contact-us" component={ContactUs} onEnter={handlePageChange}/>
  </Route>
</Router>

Also need to modify your "handlePageChange" function as below:

const handlePageChange = () => {
    window.scrollTo(0, 0);
    if ('scrollRestoration' in history) {
        history.scrollRestoration = 'manual';
    }
}
like image 125
Tanmay Verma Avatar answered Sep 20 '22 03:09

Tanmay Verma