How to navigate outside of components in ReactRouter 4?
See original GitHub issueIn React Router (v3) I can accept a server response and use browserHistory.push to go to the appropriate response page. However, this isn’t available in v4, and I’m not sure what the appropriate way to handle this is in React-Router V4 .
In below example when the server returns a success, the user is taken to the Cart page
// actions/index.js
export function addProduct(props) {
return dispatch =>
axios.post(`${ROOT_URL}/cart`, props, config)
.then(response => {
browserHistory.push('/cart'); // no longer in React Router V4
});
}
How can I make a redirect to the page from function in React Router v4?
I can find lot of references on navigation from components in my case i need to navigate from a function
Issue Analytics
- State:
- Created 6 years ago
- Reactions:26
- Comments:17 (4 by maintainers)
Top Results From Across the Web
Navigate Outside Components · React Router
If you are trying to do it in the Route component (in short, it's a component which is definied in your routes configuration)...
Read more >react router v6 navigate outside of components - Stack Overflow
1. Create a new file with your CustomRouter component. I'm placing it at "./components/CustomRouter.tsx" in this example. · 2. Import the ...
Read more >[Bug]: How to navigate outside React context in v6? #8264
Before react router v6.4, I used this way to navigating outside of components. import {createBrowserHistory} from 'history' const history ...
Read more >React Router navigate outside component - Edupala
Example of React Router navigate outside component. Step 1: Create CustomRouter and history object · Step 2: Replace BrowserRouter with the plain router....
Read more >Using 'history' to navigate your React app from outside a ...
Using 'history' to navigate your React app from outside a component · import React from 'react'; import ReactDOM from 'react-dom'; · import React...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

If you are outside of a route you can wrap your component in withRouter to get the history object.
If I’m understanding the suggestion correctly, if we need to access the history object outside of a component we basically have to create our own version of BrowserRouter and have it use a history object we’ve created. Wouldn’t it be easier on users of this package if those history objects were available like they used to be, rather than having to create a slightly modified version of something that’s already done?