-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to access router or route progmatically? #59
Comments
The users.jsx file has an example on how to transition to another route (it is passed down the context): _showProfile(seed: string) {
this.context.router
.transitionTo('profile', {seed});
} When it gets hairy is when you want to shield pages with a login screen (or the whole app). This is a react-router issue that has been discussed on remix-run/react-router#590 Basically, because this implementation using Alt (but the same applies to Flummox and MartyJS for example) uses a flux instance (more or less a must if you want to have proper isomorphic rendering) and not a singleton, you need to have access to the instance in the static method "willTransitionTo". Some options are given on that github issue page, but they all have serious issues (not to mention the code just feels and is ugly). I've resorted to a conditional render on the App component (and have the app component listen to loginChange): if (props.user) {
appComponent = <RouteHandler {...props} />;
} else {
appComponent = <Login {...props} />;
} This however brings up another issue because of the way this boilerplate fires off actions on componentWillMount/componentDidMount. React will throw errors that a new dispatch can't happen within a previous dispatch. It took me a while to find a solution, but this works (based on feedback in Fluxxor issue BinaryMuse/fluxxor#92), in main.js: import React from 'react/addons';
…
(async () => {
// Init alt instance
let flux = new Flux();
let oldDispatch = flux.dispatcher.dispatch.bind(flux.dispatcher);
flux.dispatcher.dispatch = (action) => {
React.addons.batchedUpdates(() => {
oldDispatch(action);
});
};
… I'm not completely happy with my solution, but at least it works and it works both on the server and on the client. If someone can chime in with a login solution that redirects a page to /login and after a successful login back to the originating route and get that working both on the serverside and the clientside without a forked react-router and ugly workaround, please do. This is a fairly common case with SPA (appwide authentication or authenticated routes) and a clean solution would be very welcome. |
My solution is to make two routers, a public and an authed router and then dynamically set them. I'm not sure how that will work with the cache though. |
I'm waiting react-router new API, things will be much easier. For now, I'm doing the same thing @masqita |
Hi @iam4x, I have an isomorphic auth flow issue/question with react-router for now. I created require-auth.jsx to protect pages, then run with But if I disabled Javascript, it will not working on server side, just hang on response. (should redirect to log in page.) It seems like |
@lancetw Can I have more code as example? I suspect that |
Hi @iam4x, the You could use this branch, I add a "protected" page and links, the code just warps like
If you click a link to protected page, it will be redirected to "login-info" page, but only when Javascript enable on client browser.) |
I test again (local), strangely, after disable Javascript, It's only work well on Chrome browser, If I put site on remote server, request a protected page then wait redirect, |
Finally, flummox doc example solved my question, check this: https://github.com/acdlite/flummox/blob/master/docs/src/server/appView.js |
@lancetw Nice, it would be great as addition on the boilerplate. Can you make me a PR? |
@iam4x I send the PR. :) |
Now things should be easier with new react-router beta, I've updated the boilerplate and the example of @lancetw will be improved soon. |
What's the best way to access the router from the client? Specifically I want to transition users on log in or log out. Should I add the router as a property on the alt container?
The text was updated successfully, but these errors were encountered: