maisano / react-router-transition

painless transitions built for react-router, powered by react-motion
http://maisano.github.io/react-router-transition/
MIT License
2.6k stars 138 forks source link

Incompatibility with HashRouter #68

Closed fiso closed 6 years ago

fiso commented 7 years ago

I need to use router transitions in a project that's a Cordova app. For various reasons, HashRouter works much better than BrowserRouter within the context of Cordova.

However, react-router-transition doesn't appear to work when your app is contained in a HashRouter. Is this a simple fix?

Let me know if this description is not fleshed out enough and I'll provide a code sample to demonstrate how to repro. I'm kind of assuming that you'll know what I'm talking about right away here :)

fiso commented 7 years ago

So I have a workaround in my app right now which I'll outline here, but it would be preferrable if this worked out of the box with HashRouter.

App root:

ReactDOM.render(
  <HashRouter>
    <App />
  </HashRouter>
  ,
  document.getElementById('root')
);

App component render method:

  render () {
    // This (tampering with the location object) is a potentially troublesome
    // hack. The purpose is to allow route transitions to work within
    // HashRouter. HashRouter is overall much easier to get to play nice with
    // Cordova, so we prefer to use it over BrowserRouter. To revert to
    // BrowserRouter with no transitions, replace <AnimatedSwitch> with
    // a <Switch> below, and replace HashRouter with BrowserRouter in index.js
    const newLocation = Object.assign({}, location);
    newLocation.pathname = newLocation.hash.replace('#', '');
    newLocation.hash = '';

    return (
      <main>
        <AnimatedSwitch
            location={{...newLocation, key: ''}}
            atEnter={bounceTransition.atEnter}
            atLeave={bounceTransition.atLeave}
            atActive={bounceTransition.atActive}
            mapStyles={bounceTransition.mapStyles}
            runOnMount
            className="switch-wrapper">
          <PrivateRoute path='/dashboard' component={Dashboard}
            authed={this.isAuthenticated()} />
          <Route path='/register' component={RegistrationView} />
          <Route path='/login' component={LoginView} />
          <Route path='/' component={LoginView} />
        </AnimatedSwitch>
      </main>
    );
  }
}
maisano commented 7 years ago

@fiso–thanks for opening a ticket. i haven't tried using react-router-transition with hash router since i rewrote the more v4-friendly version. from a quick glance i'm uncertain as to why it's not compatible, but i can try to have a look soon :)

maisano commented 7 years ago

hi @fiso–i just published a patch, version 1.1.1 that fixes a small issue when using HashRouter. still haven't had the chance to dive deeper here, but give that a go and see if it helps at all?

fiso commented 6 years ago

Sorry for not getting back to you earlier, but I can finally confirm that this appears to be fixed now. Thanks!