0

I am using React-router 3.2.0 so I have a button in the application on which I called goBack function of hashHistory as below

Router.hashHistory.goBack();

I works fine but I need to add some condition before it called. So my problem is like if I move between 3 links

  1. Home
  2. Dashboard
  3. Settings

Initially it will be on Home then I moved to Dashboard then Settings. Now I clicked on button on which I called goBack() function at that time current history location is Settings it moved to Dashboard then Home after reaching at Home there is no such history locations exist.

So this is I need to check is there any history location still exist or not. Something like

if(/*history exist*/){
 call goBack();
} else {
 //do my stuff
}

Is there any way to do that in react-router 3.2.0 version ?

Tholle
  • 108,070
  • 19
  • 198
  • 189
Kushal Jain
  • 3,029
  • 5
  • 31
  • 48

1 Answers1

0

The history object in react-router has a length property indicating how many entries are in the stack.

if (history.length !== 0) {
 goBack();
} else {
 // do my stuff
}
Tholle
  • 108,070
  • 19
  • 198
  • 189
  • 1
    Are you talking about window.history or any History object available in the react-router ? – Kushal Jain Jun 27 '18 at 11:27
  • @KushalJain The history object available in react-router. – Tholle Jun 27 '18 at 11:28
  • I tried like import {History} from 'react-router'; then console.log(History) it occurs undefined. – Kushal Jain Jun 27 '18 at 11:32
  • @KushalJain The `history` object is not exported from `react-router` directly, but you can get it [like described in this answer](https://stackoverflow.com/questions/42672842/how-to-get-history-on-react-router-v4). – Tholle Jun 27 '18 at 11:39