1

Wondering how to go back to a previous route in a react web app using hashRouter instead of browserRouter in react-router v4?

I've found this question which doesn't seem to work even though it says no browser mixin needed (plus I think its talking about an older react-router version) however, every other solution I've seen depends on browserHistory.

Is it possible with hashHistory?

WiseOlMan
  • 926
  • 2
  • 11
  • 26

2 Answers2

5
this.props.history.goBack()

Taken from the comments on this question

It is a function call.

Kyle Pennell
  • 5,747
  • 4
  • 52
  • 75
WiseOlMan
  • 926
  • 2
  • 11
  • 26
  • I used this into my homepage this.props.history.push({ pathname: '/Classlist'}) and this.props.history.goBack() into Classlist page it work great but when this.props.history.push({ pathname: '/classdetail'}) when i used this.props.history.goBack() into classdetail page it's redirect to exact path that is '/' – Manoj Bhardwaj Aug 31 '18 at 07:48
1

Well in my case i did like that :

import withRouter from "react-router-dom/es/withRouter";
import React from "react";

class Component extends React.Component {
  goBack() {
    this.props.history.go(-1);
  }
  ...
}    
const WrappedComponent = withRouter(Component)
export default WrappedComponent;

withRouter give us access to history in props of component, but i'm not sure is this way is correct

ventro_art
  • 574
  • 1
  • 4
  • 16