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');
});
}
}
但在控制檯登錄後出現錯誤: 「警告:失敗的上下文類型:提供給Login
object
型的無效的情況下router
,預計function
檢查。渲染方法RouterContext
「。
在我看來,我通過了func
。不是object
類型,所以我不知道哪兒來這個錯誤...
你有沒有嘗試console.log來查看它是否是一個函數? 'this'可能不是您認爲它在onClick中的內容 – hansn