当我尝试将路由器传递给组件登录时,我收到了这样的错误:
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
类型,所以我不知道这个错误来自哪里......
最佳答案
路由器
是一个对象:
试试这个:
static contextTypes = {
router: React.PropTypes.object.isRequired
}
关于reactjs - react 路由器和上下文类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37242135/