I am writing a authentication module in Flux, actions : auth,auth_success,auth_error. I am thinking when action auth_error occur, the router will go to '/login'. When action, action, auth_success occur, the router will go to '/dashboard'. But it seems to be wrong because action only goes to dispatcher. I don't know how to do route the callbacks. Maybe check the store value?
Asked
Active
Viewed 413 times
0
-
There are no routing packages in Flux or React. This means that you are most likely handling routing with something else. The easiest way to solve your problem is to just route the user after the authentication have happened. This can be done easily with JS. Just change the window location to fit your needs. Then let the router define which components to render. – magnudae Mar 06 '15 at 09:01
1 Answers
0
You have to mixin
your React class with Route.Navigation object, for instace
/** @jsx React.DOM */
var React = require('react');
var Router = require('react-router')
, Navigation = Router.Navigation;
var UserStore = require('user-store');
var YourClass = module.exports = React.createClass({
mixins:[Navigation], //This is very important
getInitialState: function() {
return {};
},
componentWillMount: function(){
UserStore.addChangeListener(this._method);
},
componentWillUnmount: function(){
UserStore.removeChangeListener(this._method);
},
render: function() {
return (
<div>
</div>
);
},
_method: function() {
// From here you can call methods of Navigator Object
this.transitionTo('SomeRouteName'); //This method will render the specified <Route>
}
});
For further information you can check https://github.com/rackt/react-router/blob/master/docs/api/mixins/Navigation.md
In order to change the route and according to flux architecture, you should call transitionTo
from a callback of some User Store you should have.
I added an example to the code, you may customise it to your specific case.
Happy coding!

Nytramr
- 84
- 4
-
i know how to use react-router, what i do not know is how to use it an action callback in Flux, case it is not control by react component. – Chopper Lee Mar 09 '15 at 03:48