Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get previous paths from ReactRouter.browserHistory

Tags:

react-router

Is it possible to get the previous number of paths and the path values from ReactRouter.browserHistory?

like image 681
Noitidart Avatar asked Nov 23 '16 01:11

Noitidart


People also ask

How do I get past path in react router?

To detect previous path in React Router, we can set the state property to an object with the location. pathname as the value. <Link to={{ pathname: "/nextpath", state: { prevPath: location.

How do I get past route in react router 6?

To go back to previous route in react-router-dom v6 First of all, you need to import useNavigate from react-router-dom and then you can use navigate(-1) to go back to the previous version If You want to Go 2 pages back then probably you have to use navigate(-2).

How do I use browserHistory in react router?

React Router has a useHistory hook that provides a history interface that we can easily use for routing. Add buttons to these pages as shown below to add routing with the History API. The snippet above uses the goBack() method to mimic the back button in the browser and the push() method to move to a new route.


1 Answers

Actually you can't. But you can try storing a list in browserHistory state and pushing new path in it when you redirect.

let history = this.props.location.state.history;
history.push(this.props.location.pathname)
browserHistory.push({
    pathname: '/yourpath',
    state: { history: history }
})

in this way you should find all your history in this.props.location.state.history

like image 58
Daniele Sciuto Avatar answered Sep 29 '22 11:09

Daniele Sciuto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!