2016-05-15 65 views
2

我收到了這樣的錯誤,當我嘗試路由器傳遞到組件登錄:反應路由器和上下文類型

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類型,所以我不知道哪兒來這個錯誤...

+0

你有沒有嘗試console.log來查看它是否是一個函數? 'this'可能不是您認爲它在onClick中的內容 – hansn

回答

5

router是一個對象:

試試這個:

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

指定下列內容:

router: function() { React.PropTypes.func.isRequired }