You do not need to downgrade to v3, React-Router 4.0.0 is fully capable of fulfilling what the OP requires.
const history = createBrowserHistory();
is a custom history object, so you should use <Router> to synchronize with the <BrowserRouter> router instead of <BrowserRouter> , which I assumed you used.
Try this instead:
import React, {Component} from 'react'; import { Router } from 'react-router'; import { Route } from 'react-router-dom'; import createHistory from 'history/createBrowserHistory'; const history = createHistory(); class App extends Component { constructor(props){ super(props); } render(){ return ( <Router history={history}> //pass in your custom history object <Route exact path="/" component={Home} /> <Route path="/other" component={Other} /> <Router /> ) } }
Once your custom history object is passed through the router's history prop, history.push should work as expected, anywhere in your application. (you can put your history object in the history configuration file and import it in the places where you want to route programmatically)
Hope this help!
For more information, see: Restore Router History Object
Tina chen
source share