score:2

Accepted answer

Coming from an Angular background I had incorrectly assumed that routing was happening entirely client-side. It's possible to use react-router for both client-side and server side routing.

The transition.to call worked fine on the client-side, but threw an exception when server-side routing kicked in and returned the Cannot GET / page. The exception was being caught but nothing logged.

I added an if statement to see if we were running on the client:

if(window.location) {
    if(!loggedIn) {
        transition.to("/login");
    }
 }

There is probably a better way to handle this and if I figure that out eventually I'll update my answer.

score:0

According to the current docs, the onEnter hook receives a replace function as its second param. i.e.

type EnterHook = (nextState: RouterState, replace: RedirectFunction, callback?: Function) => any;

Are you using a different version of react-router? I tried looking through the alternate version docs for an onEnter hook that takes a transition object as its second param, but I didn't have much luck in finding anything.


Related Query

More Query from same tag