-
Notifications
You must be signed in to change notification settings - Fork 154
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
Cannot get reference to Router before rendering and can't add store without router reference #132
Comments
I'm not sure a route store is really the correct technique in most cases; the React Router example in the Fluxxor docs utilizes it mainly because models are not created asynchronously in action creators, unlike a more traditional Fluxxor app. Perhaps this sets a bad precedent. Additionally, in React Router 1.0 beta, I'm not sure there's a way to access the router instance before the React rendering flow (via I think a more common technique, especially for situations like yours, would be to simply access the router on componentWillMount: function() {
if( this.state.session ) {
this.context.router.transitionTo("/dashboard");
}
},
contextTypes: {
router: React.PropTypes.object
}, If you really want/need to utilize a store for routing operations, and just can't get ahold of the router instance, you might consider passing it along from the component: componentWillMount: function() {
if( this.state.session ) {
flux.actions.route.transition(this.context.router, "dashboard");
}
},
contextTypes: {
router: React.PropTypes.object
}, Another (very hacky) workaround to the don't-have-the-router-instance-yet is to set it first thing from the // In the App component
componentWillMount: function() {
var flux = this.getFlux();
if (!flux.store("RouteStore")) {
flux.addStore("RouteStore", new RouteStore({router: this.context.router}));
}
},
contextTypes: {
router: React.PropTypes.object
}, Finally, I'm still not convinced the technique I originally used for the React Router example (before changing it) is actually that bad a practice; generally actions should be fire-and-forget, but for very side-effecty things like routing I'm not as much of a stickler: doThing: function() {
flux.actions.thing.create(this.state.data, function(id) {
this.context.router.transitionTo("/things/" + id);
}.bind(this));
},
contextTypes: {
router: React.PropTypes.object
}, |
In React Router 1.0 RC1, they show how you can get the
This is analagous to getting a reference to the router before; You can use the history API to change the page. See also the API described at "Navigation Mixin". |
I am trying to add a RouteStore but can't seem to get the router reference. I am running into issues where the Login component renders and detects the user is already logged in and shoots off an action to redirect but the Router store is not ready to receive it yet.
When the Login component mounts though I do a check:
However the Login component mounts before the Store can be added so I get
The text was updated successfully, but these errors were encountered: