question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to navigate outside of components in ReactRouter 4?

See original GitHub issue

In 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:closed
  • Created 6 years ago
  • Reactions:26
  • Comments:17 (4 by maintainers)

github_iconTop GitHub Comments

31reactions
wdjungstcommented, Jun 14, 2017

If you are outside of a route you can wrap your component in withRouter to get the history object.

10reactions
mrobriancommented, Jun 14, 2017

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found