larrymyers / react-mini-router

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

Is it possible to execute redirections? #56

Closed igorsantos07 closed 8 years ago

igorsantos07 commented 8 years ago

It seems, because of the project's simplicity, this is not possible, but I'm asking anyway as I'm a newbie :)

My idea is to prohibit access to some routes given criteria (such as unauthenticated user). I'm currently using navigate() inside the routes, but it yields errors as it changes the state during the rendering call...

larrymyers commented 8 years ago

If you need to re-route due to an invalidate state (i.e. unauthenticated users), the best thing I've found is to use conditional logic in the route handler to render some sort of loading graphic, and then in a setTimeout call redirect to the correct route.

The setTimeout allows the rendering pass to complete before you change state and reroute. Changing state while a render is in progress will cause React to appropriately yell at you about it.

igorsantos07 commented 8 years ago

Thanks man! It was not the easiest thing to do but this is a mini router, so I can't ask much. Thanks for the quick response :)