How do I access current path in a FLUX store?
Using react-router with the FLUX architecture, how does one access the current path/params/query from a store? Is there a mixin or some kind of action I can listen to? Am I missing something stupid?
You can send the data include your current router's params to your store.
Do you mean just manually raise an action with the params in the render method of the handler?
No, I mean when you send a action, you can send the params in your payload.
Can you explain from a slightly bigger picture where this action would be raised? I understand fine how to pass parameters with an action, but I do not understand where this action should be raised. I have to specify a handler for a route, which must be a component. This provides my component with the URL params/query in the props. At what point should I be raising an action with these params/query in the payload? Is it not an anti-pattern to raise them in the component's render function?
I can explain how i do this .
Usually we have two action viewAction and serverAction.
viewAction is handle the view states,
serverAction is handle the server response states.
For example:

Wish I had seen that diagram before getting totally confused about flux and async operations :)
Anyway, I think @tom-james-watson question is where to fire an action or populate a store with active route information after every transition. Router.onChange seems like a good place to me. If/When Router.run gets merged, it seems you should have more control, like firing the action before/after render/data loading, firing multiple actions etc.
thanks @xeodou for this nice diagram. Maybe my case extends to this and I would love to see a comment on this: I have a form with a save button, working in way you described it. I want to redirect to another route if the api call succeeded. I'm curios about place to do this. Since I understand the router's navigation as ActionCreator I want do do this in the component, but I have no clue how to picture that in my stores.
Edit: I think the answer is in #380
@mosch You can define a value that if the form submit is succeed, make it true or make it false , If you have listener the onChange event , you can check the value , if true redirect to another router.
Or maybe you can just create another event called submitListener , then listener this event in your component , When your server action callback trigger this event, then you will get the trigger in your component then redirect another page.
@xeodou thanks for the quick reply! since i work with promises i was thinking about returning the promise from the action creator and when it succeeds, do the redirect. im not sure if this violates the unidirectional data flow, but also i can't see a drawback with this approach.
Here's what I've ended up doing and a better explanation of what I was trying to do:
I want to set state in multiple stores when the page first loads based on URL query parameters. There doesn't appear to be a proper way for a store to access the current route information directly, it looks like this can only be done by components.
I only have one route handler, which makes things easy. In this component's componentDidMount method, I raise an Action that contains this.props.query. The relevant stores listen for this action and update their state accordingly. If you need to keep sending the route props as they change (I didn't), then you can raise an action in componentWillReceiveProps as well.
I think this may be a bit of FLUX antipattern, but it seems to be working and I can't find a better way of doing this.
Using componentDidMount is not just an antipattern but I've had that break on me. If the route transition is initiated because of a state change in your Flux store there are subtle ways you can end up in an infinite callback loop. The Fluxxor library (which I use) explicitly forbids this by not allowing me to dispatch an action while handling another one.
statics.willTransitionTo is where I've seen the most success, but you still need to be careful - the only way that one will work is if you have global access to your app's Flux (the view component will not yet be instantiated).
Technically speaking what we've got here is two separate flux systems working alongside each other. System A is the Router, System B is your app's Flux. Only the views can tell the stores what to do - any attempt to wire the two stores directly to each other is an antipattern and will start to violate all those nice guarantees you get when you follow the Flux pattern closely.
Also keep in mind that you have access to window.location.hash when initializing your stores; no reason why you need to wait for the Router to tell you the query params...
Thanks for your response.
It seems like a shame to have to manually retrieve the parameters from window.location.hash, duplicating functionality from react-router. It seems like it should be quite basic functionality to be able to access the currrent route to initialize stores on first page load.
Another similar thing I'm having an issue with - if I want to both raise an action through my own dispatcher as well as changing route when I click on a button, what is the best way to achieve this? I was trying to use a < Link /> that also had an onClick that raised an action but this didn't work. I've ended up giving the component the Navigation mixin, passing a reference to the component in my action and calling component.transitionTo from a store (as per #380). Yuck! Is that really the best way to do this?
For your link problem, the way that I'm currently handling this is to have an onClick event on the link or button. The onClickHandler then calls any relevant actions and then calls transitionTo. I'm not sure if this is the prescribed way to do it but it seemed the most logical and easy to follow and maintain.
in the v0.11 you can provide the path to a store before rendering anything:
Router.run(routes, function(Handler, state) {
// send the path to an action, then stores get it from the payload
RouteActions.pathChange({path: state.path});
React.render(<Handler/>, document.body);
});