reactjs - react 路由器和上下文类型

标签 reactjs react-router

当我尝试将路由器传递给组件登录时,我收到了这样的错误:

class Login extends React.Component {

onClick(){
  Actions.login(this.context.router);
}

static contextTypes = {
  router: React.PropTypes.func.isRequired
}

render(){

    return (
        <Card style={{
          'maxWidth': '800px',
          'margin': '30px auto',
          'padding': '50px'
        }}>
          <CardText style={{
            'textAlign': 'center'
          }}>
            To start chatting away, please log in with your Google account.
          </CardText>

          <RaisedButton style={{
            display: 'block',
          }} onClick={this.onClick.bind(this)}
          label="Log in with Google" primary={true} />
        </Card>
    );
   }
 }

export default Login;

然后我尝试将其传递给操作login,以便在记录后重定向到其他路由。 操作登录:

login(router){
return (dispatch) => {
  let firebaseRef = new Firebase('https://front-react-stack.firebaseio.com');
  firebaseRef.authWithOAuthPopup("google", (error, user) => {
    if(error){
      return;
    }

    dispatch(user);

    router.push('/chat');
  });

}

}

但是登录控制台后出现错误: “警告:失败的上下文类型:提供给 Loginobject 类型的上下文 router 无效,预期的 function。检查RouterContext 的渲染方法。”

在我看来,我已经通过了func。不是 object 类型,所以我不知道这个错误来自哪里......

最佳答案

路由器是一个对象:

试试这个:

static contextTypes = {
  router: React.PropTypes.object.isRequired
}

关于reactjs - react 路由器和上下文类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37242135/

相关文章:

javascript - ReactJs - 使用 Object.keys().map 搜索过滤器 - 未捕获的 TypeError : this. state.texts.filter 不是 t.value 处的函数

reactjs - React、Redux、React-Router?

seo - hashHistory、_escaped_fragment_ 和 Google

reactjs - React Router 4 正则表达式路径 - 匹配找不到参数

javascript - JWT 过期时注销用户

html - 如何修复我的 React/ElectronJS View 的大小?

javascript - handleClick 函数不适用于父组件?

javascript - 根据状态或点击事件更改文本颜色

redux - react + Redux + SSR : Cannot find store only when navigating directly to connected component

express - 在 Isomorphic React 上处理 SEO