问题在于状态未定义。 nextProps.userid的第一个控制台日志显示正确的用户标识,但它没有更改状态,第二个控制台日志返回null。
class Profile extends React.Component {
constructor(props) {
super(props);
this.state = {
user: this.props.userid,
};
}
componentWillReceiveProps(nextProps){
console.log('PROFILE USERID', nextProps.userid)
const userid = nextProps.userid
this.setState({user:userid})
console.log('PROFILE USERID 2', this.state.user)
}
答案 0 :(得分:0)
setState
是异步的。因此,您不能指望在setState
之后立即更改状态值。如果您在setState
之后立即记录您的状态,那么它将显示您之前的状态。如果您想查看状态的更新值,请尝试将回调传递给this.setState({user:userid}, () => {
console.log('PROFILE USERID 2', this.state.user)
});
,如此
if(await userManager.UpdateSecurityStampAsync(user.Id) != IdentityResult.Success)
{
// https://www.stevejgordon.co.uk/asp-net-core-identity-token-providers
// we update it to effectively reset all the token validation stuff
return IdentityResult.Failed("failed to update the security stamp");
}
// Send token - this may throw but thats ok as we just rollback
string code = await userManager.GenerateTwoFactorTokenAsync(user.Id, "twilio");
await userManager.SmsService.SendAsync(new Microsoft.AspNet.Identity.IdentityMessage
{
Destination = user.UserName,
Body = "Your security code is: " + code
});