react-router icon indicating copy to clipboard operation
react-router copied to clipboard

Using the Navigation mixin in a Flux architecture versus the removed `Router` transition methods

Open ryanatkn opened this issue 12 years ago • 42 comments

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?

ryanatkn avatar Oct 10 '14 04:10 ryanatkn

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

klimashkin avatar Oct 10 '14 06:10 klimashkin

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.

ryanflorence avatar Oct 10 '14 07:10 ryanflorence

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');

ryanflorence avatar Oct 10 '14 14:10 ryanflorence

@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?

ryanflorence avatar Oct 10 '14 14:10 ryanflorence

Thanks for the explanation - using the <Routes/> instance looks good.

ryanatkn avatar Oct 10 '14 14:10 ryanatkn

Thanks for this. Took me a while to find it but solved my problems. I think this should be explained somewhere.

victormier avatar Oct 21 '14 16:10 victormier

@rpflorence is this method of storing rendered instances still valid in 0.11?

justinbelcher avatar Nov 24 '14 04:11 justinbelcher

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);
});

ryanflorence avatar Nov 24 '14 05:11 ryanflorence

added a quick guide https://github.com/rackt/react-router/blob/master/docs/guides/flux.md

ryanflorence avatar Nov 24 '14 05:11 ryanflorence

Sweet, thanks @rpflorence

justinbelcher avatar Nov 24 '14 22:11 justinbelcher

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 avatar Dec 01 '14 15:12 klimashkin

@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.

ryanflorence avatar Dec 01 '14 15:12 ryanflorence

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.

ryanflorence avatar Dec 01 '14 15:12 ryanflorence

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.

arthuryeti avatar Dec 04 '14 05:12 arthuryeti

@Atu typically, if your loader follows node conventions, an empty object returned from require() denotes a circular dependency.

justinbelcher avatar Dec 04 '14 16:12 justinbelcher

@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.

arthuryeti avatar Dec 04 '14 19:12 arthuryeti

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?

2color avatar Jan 19 '15 14:01 2color

@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 avatar Jan 29 '15 00:01 JoelOtter

@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 avatar Jan 29 '15 00:01 gaearon

@gaearon - Brilliant, thank you.

JoelOtter avatar Jan 29 '15 13:01 JoelOtter