title: Check history previous location before goBack() react router v4
body: <p>My goal is to enable a "Go Back" button, but only if the route/path the user would go back to is in a certain category. </p>
  
  <p>More precisely I have two kinds of Routes : <code>/&lt;someContent&gt;</code> and <code>/graph/&lt;graphId&gt;</code>. When the user navigates between graphs they should be able to go back to the previous graph but not to a <code>/...</code> route. This is why I can't simply run <code>history.goBack()</code>, I need to check the location first.</p>
  
  <pre><code>const history = createHashHistory();
  const router = (
          &lt;ConnectedRouter history={history}&gt;
              &lt;Switch&gt;
                  &lt;Route exact path='/' component={Home}&gt;&lt;/Route&gt;
                  &lt;Route exact path='/extension' component={Home}&gt;&lt;/Route&gt;
                  &lt;Route exact path='/settings' component={Home}&gt;&lt;/Route&gt;
                  &lt;Route exact path='/about' component={Home}&gt;&lt;/Route&gt;
                  &lt;Route exact path='/search' component={Home}&gt;&lt;/Route&gt;
                  &lt;Route exact path='/contact' component={Home}&gt;&lt;/Route&gt;
                  &lt;Route path='/graph/:entityId' component={Graph}&gt;&lt;/Route&gt;
              &lt;/Switch&gt;
          &lt;/ConnectedRouter&gt;
  );
  </code></pre>
  
  <p>I'd like to implement something like this in the <code>Graph</code> component:</p>
  
  <pre><code>if (this.props.history.previousLocation().indexOf('graph') &gt; -1){
      this.props.history.goBack();
  } else {
      this.disableReturnButton();
  }
  </code></pre>
  
  <p>This question also applies to <code>goForward()</code> as I'd like to disable the "forward" button if not applicable. Also the user moves around with <code>this.props.history.push(newLocation)</code></p>
  
  <p>Obviously I'd like to avoid using side-tricks like logging in <code>localStorage</code> or a redux <code>store</code> as the user moves around, it feels less natural and I know how to do it.</p>