Using the Navigation mixin in a Flux architecture versus the removed `Router` transition methods
I'm having trouble seeing how replacing the functions Router.transitionTo, replaceWith, and goBack with the Navigation mixin is compatible with a Flux architecture. Let's say I have a component that creates a new post. I hit the create button and a component responds by firing off an action. In 0.7.x or before, I'd use Router.transitionTo in either the action or store layers when the action was successful. In 0.9.x and beyond, a component needs to do this. How should the component know to make the transition?
One solution would be to pass the actions all the way through so the component knows what to do. Another solution would be to provide a callback from the component when the action is created that would be called when the transition should happen. Yet another would be to have some special store state that the component would read to trigger the transition, and then immediately reset that state. None of these solutions seem particularly elegant or fluxy to me, but I'd be interested in hearing any recommendations. A callback is probably my favorite of the three but it feels like a reversal of data flow - Flux heresy?
Faced with the same problem
It would be nice if there were methods willTransitionTo (before route change for data prefetching or whatever this method will be named) and didTransiotionTo (after route change if willTransitionTo didn't call abort) in components. And if <Routes> onChange fired before render if using some flux store for additional processing which depends on navigation state https://github.com/rackt/react-router/issues/377
Your rendered <Routes/> instance will have these methods.
Its a little bit of boilerplate, but pretty common to need to hang onto instances this way w/o creating circular dependencies. Should maybe even call it a container, not a store, but anyway:
// main.js
var React = require('react');
var routes = require('./routes.js');
var RouterStore = require('./RouterStore');
var AppRouter = React.renderComponent(routes, document.body);
RouterStore.set(AppRouter);
// RouterStore.js
var _router = null;
exports.set = function(router) {
_router = router;
};
exports.get = function() {
return _router;
};
// SomeActionCreator.js
var RouterStore = require('./RouterStore');
module.exports = {
doStuff: function(component) {
RouterStore.get().transitionTo('foo');
}
};
Thanks for asking the question, I should probably add this to some docs somewhere, it'll be common question.
As for "WHY DID YOU DO THIS?!". We can't have singleton things, need all this stuff scoped to an individual routes instance for server-side rendering.
A secondary option is to simply send the component in the action payload:
SomeActionCreator.doStuff({stuff: stuff: caller: this});
// in the action handler
payload.caller.transitionTo('somewhere');
@klimashkin
Can you help me understand how the things you mentioned here are related to the switch from singleton router methods to the navigation mixin?
Thanks for the explanation - using the <Routes/> instance looks good.
Thanks for this. Took me a while to find it but solved my problems. I think this should be explained somewhere.
@rpflorence is this method of storing rendered instances still valid in 0.11?
its much better now:
///////////////////////////////////////////////////////////
// router.js
var routes = require('./routes');
var Router = require('react-router');
// we can create a router instance before "running" it
var router = Router.create({
routes: routes,
location: Router.HistoryLocation
});
module.exports = router;
///////////////////////////////////////////////////////////
//SomeActions.js
// and then action creators can require it like other
// singletons in the app
var router = require('../router');
module.exports = {
doSomeTransitionThing: function () {
router.transitionTo('somewhere');
}
};
///////////////////////////////////////////////////////////
// main.js
// finally, run it in your main script
var React = require('react');
var router = require('./router');
router.run(function (Handler) {
React.render(<Handler/>, document.body);
});
added a quick guide https://github.com/rackt/react-router/blob/master/docs/guides/flux.md
Sweet, thanks @rpflorence
It seems that the router is still not suitable for Flux-architecture.
If each component subscribe to specific Store, after route change inside 'run' handler we call fetch all data for the current paths that cause Stores filling, which will cause a render of subscribed components. And after that will be called React.render(<Handler />, document.body); which again redraw the entire page with all components.
What's you vision for this?
@klimashkin depends on how and when you're fetching data, additionally, you should have caching in your data layer anyway so that multiple calls to the same resource don't cause a new request unless its stale.
What's you vision for this?
I'd have to see your code to provide more detailed help, since "flux" is an idea, not a prescription that I can talk directly about, nor does it address data caching.
I guess I can speak briefly to this. All the flux demos I've seen do data fetching in componentDidMount of a component. If some higher level component is already mounted, and the route changes, and the component is still active, componentWillReceiveProps will be called, but componentDidMount will not, and so it's not going to refetch the data.
I have tried to use your guides for flux architecture, but it seems to my require('../router')return an empty object. I have used the same structure that your provide in the guide. It seems that the router.run is not yet run in my commonJS environment.
@Atu typically, if your loader follows node conventions, an empty object returned from require() denotes a circular dependency.
@jbelcher It could be something like that, because my routes are calling from an other file into the router, but I call the router in my actions.
Maybe I'm doing something wrong.
I guess I can speak briefly to this. All the flux demos I've seen do data fetching in componentDidMount of a component. If some higher level component is already mounted, and the route changes, and the component is still active, componentWillReceiveProps will be called, but componentDidMount will not, and so it's not going to refetch the data
@rpflorence What's your recommendation in this context?
@Atu Did you solve your issue? I've run into exactly the same thing using rpflorence's given code.
It appears there is a circular dependency - my store requires the router, the router requires the routes, and the routes file requires lots of things. Might try using the mixin inside a toplevel component and passing something through the state to get it to change, but that feels like a bit of a hack.
@JoelOtter I use this:
'use strict';
var router;
// Here's the trick: assign to module.exports before any require()s
module.exports = {
getCurrentPath() {
return router.getCurrentPath();
},
makePath(to, params, query) {
return router.makePath(to, params, query);
},
makeHref(to, params, query) {
return router.makeHref(to, params, query);
},
transitionTo(to, params, query) {
router.transitionTo(to, params, query);
},
replaceWith(to, params, query) {
router.replaceWith(to, params, query);
},
goBack() {
router.goBack();
},
run(render) {
router.run(render);
}
};
var routes = require('./routes'),
Router = require('react-router');
router = Router.create({
routes: routes,
location: Router.HistoryLocation
});
@gaearon - Brilliant, thank you.