gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 14:01:31 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com