如何使用React-router-component从ajax调用成功后重定向?

Tor*_*tto 9 reactjs reactjs-flux

我正在使用Facebook flux架构构建应用程序React JS.我已经构建了app的基本部分,我有一个登录表单.我从节点服务器获取结果以验证商店中的用户,我从服务器获得结果,现在我卡住了,如何在成功后将用户重定向到主页.

我已经阅读了有关反应路由器组件的信息,我能够在客户端重定向,但在从Store中的ajax获取结果时无法重定向.请帮我.

kul*_*esa 9

您需要使用mixin中的transitionTo函数Navigation:http://git.io/NmYH.它会是这样的:

// I don't know implementation details of the store,
// but let's assume it has `login` function that fetches
// user id from backend and then calls a callback with 
// this received id
var Store = require('my_store');
var Router = require('react-router');

var MyComponent = React.createClass({
  mixins: [Router.Navigation],

  onClick: function() {
    var self = this;
    Store.login(function(userId){
      self.transitionTo('dashboard', {id: userId});
    });
  },

  render: function() {
    return: <button onClick={this.onClick}>Get user id</button>;
  }
});
Run Code Online (Sandbox Code Playgroud)

  • 我刚刚注意到你正在使用`react-router-component`而不是`react-router`.我的错!我个人没有使用`react-router-component`,但是很快查看源代码,而不是`Navigation` mixin和`transitionTo`函数,你应该将`NavigatableMixin`包含在你的内容中然后使用`navigate('my_url`) )``做一个重定向. (2认同)