Use history.push in action creator with react-router-v4?

Instead of using BrowserRouter you could use the Router with custom history like import { Router } from ‘react-router’ import createBrowserHistory from ‘history/createBrowserHistory’ export const history = createBrowserHistory() <Router history={history}> <App/> </Router> in which case your history.push() will work. With BrowserRouter history.push doesn’t work because Creating a new browserHistory won’t work because <BrowserRouter> creates its … Read more

React doesn’t reload component data on route param change or query change

Along with componentDidMount, You also need to implement the componentWillReceiveProps or use getDerivedStateFromProps(from v16.3.0 onwards) in Products page since the same component is re-rendered with updated params and not re-mounted when you change the route params, this is because params are passed as props to the component and on props change, React components re-render and … Read more

Detect Route Change with react-router

You can make use of history.listen() function when trying to detect the route change. Considering you are using react-router v4, wrap your component with withRouter HOC to get access to the history prop. history.listen() returns an unlisten function. You’d use this to unregister from listening. You can configure your routes like index.js ReactDOM.render( <BrowserRouter> <AppContainer> … Read more

Nesting Routes in react-router v4

IndexRoute and browserHistory are not available in the latest version, also Routes do not accept children Routes with v4, Instead, you can specify Routes within the component Itself import { Switch, BrowserRouter as Router, Route, Redirect } from ‘react-router-dom’ render(( <Router> <Switch> <Route exact path=”https://stackoverflow.com/” component={ Main }/> <Redirect from=’*’ to=”https://stackoverflow.com/” /> </Switch> </Router> ), … Read more

Programmatically navigating in React-Router v4

You are using react-router v4, so you need to use withRouter with your component to access the history object’s properties, then use history.push to change the route dynamically. withRouter: You can get access to the history object’s properties and the closest ‘s match via the withRouter higher-order component. withRouter will re-render its component every time … Read more