larrymyers / react-mini-router

A minimal URL router for React.js
MIT License
282 stars 38 forks source link

Track history for easily going back routes #53

Closed hayesgm closed 7 years ago

hayesgm commented 8 years ago

For being able to navigate forward or backward without using History.go(-1), I added a simple pathHistory state that tracks the users page changes whenever we setState(). My preference would be to do this in the component this is mixed into, but as far as I can tell, as we're calling setState() directly, there's no way in React to hook into and add in this feature from the component react-mini-router is mixed into.

Usage

if (this.state.pathHistory.length > 0) {
  var back = <a href={this.state.pathHistory.slice(-1)[0]}>Back</a>
}

I'd be happy to hear other suggestions on how to achieve the same goal.

larrymyers commented 7 years ago

Rather than track state in the mixin, the intent was let the browser history do its thing so the back button continues to work. This never removes entries from the path history, or syncs with the current url in the browser.

This will lead to odd behavior since the browser back/forward buttons will navigate to different page state than a 'Back' link in the page.