React-无法使用context.router重定向页面

时间:2016-05-12 11:41:45

标签: reactjs react-router

我想构建一个我的react / react-router / flux应用程序。 当用户输入秘密链接(如管理页面)

时,我想将用户重定向到登录页面

这是admin组件:

TypeError: Cannot read property 'transitionTo' of undefined
at App.render (App.js:8:5)
at [objectObject].ReactCompositeComponentMixin._renderValidatedComponentWithoutOwnerOrContext (D:\web\MVCmodel\library\lib\node_modules\react\lib\ReactCompositeComponent.js:587:34)
at [object Object].ReactCompositeComponentMixin._renderValidatedComponent (D:\web\MVCmodel\library\lib\node_modules\react\lib\ReactCompositeComponent.js:607:32)
at [object Object].wrapper [as _renderValidatedComponent] (D:\web\MVCmodel\library\lib\node_modules\react\lib\ReactPerf.js:66:21)
at [object Object].ReactCompositeComponentMixin.mountComponent (D:\web\MVCmodel\library\lib\node_modules\react\lib\ReactCompositeComponent.js:220:30)
at [object Object].wrapper [as mountComponent] (D:\web\MVCmodel\library\lib\node_modules\react\lib\ReactPerf.js:66:21)
at Object.ReactReconciler.mountComponent (D:\web\MVCmodel\library\lib\node_modules\react\lib\ReactReconciler.js:37:35)
at [object Object].ReactCompositeComponentMixin.mountComponent (D:\web\MVCmodel\library\lib\node_modules\react\lib\ReactCompositeComponent.js:225:34)
at [object Object].wrapper [as mountComponent] (D:\web\MVCmodel\library\lib\node_modules\react\lib\ReactPerf.js:66:21)
at D:\web\MVCmodel\library\lib\node_modules\react\lib\ReactServerRendering.js:42:38
at ReactServerRenderingTransaction.Mixin.perform (D:\web\MVCmodel\library\lib\node_modules\react\lib\Transaction.js:136:20)
at Object.renderToString (D:\web\MVCmodel\library\lib\node_modules\react\lib\ReactServerRendering.js:40:24)
at D:\web\MVCmodel\library\lib\server.js:326:27
at D:\web\MVCmodel\library\lib\node_modules\react-router\lib\match.js:58:5
at D:\web\MVCmodel\library\lib\node_modules\react-router\lib\useRoutes.js:120:15
at done (D:\web\MVCmodel\library\lib\node_modules\react-router\lib\AsyncUtils.js:49:19)

但是,我有反应背景的问题

// to be accessible form an anonymous class variable must be declared as final!
void addListener(final JTextField jt){ 
    jt.addActionListener(new ActionListener (){
        public void actionPerformed(ActionEvent e) {
            //change some value here!
            myAction(jt);
        }
    });
}

我对这个问题没有任何想法,也不知道如何修复它。请帮助我?

1 个答案:

答案 0 :(得分:0)

好像你在react-router中使用了browserHistory(pushState API)。我会使用它的推送功能,让路由器转换到你的组件:

var Router = require('react-router');
Router.browserHistory.push('/login');

或在ES6语法中:

import {browserHistory} from ('react-router');
browserHistory.push('/login');

因此,考虑到这一点,您的完整代码应该更像这样:

import React, {PropTypes, Component} from 'react';
import {browserHistory} from 'react-router';

class App extends Component {

  render() {
    if (!adminLogin) {
      browserHistory.push('/login');
    }
    return (
      <div className="wrapper">
        <Navbar />
        <SidebarLeft />
        <div className="content-wrapper">
          {this.props.children}
        </div>
        <Footer />
      </div>
    );
  }
}

export default App;