Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 反应状态挂钩:我不能';t设置状态_Javascript_Reactjs_Firebase_Firebase Authentication - Fatal编程技术网

Javascript 反应状态挂钩:我不能';t设置状态

Javascript 反应状态挂钩:我不能';t设置状态,javascript,reactjs,firebase,firebase-authentication,Javascript,Reactjs,Firebase,Firebase Authentication,这是密码 import React,{useState}来自“React”; 从“firebase”导入firebase; const GoogleAuth=()=>{ const[user,setUser]=useState({}); const[isSignedIn,setIsSignedIn]=useState(null); var provider=new firebase.auth.GoogleAuthProvider(); provider.addScope('https://ww

这是密码

import React,{useState}来自“React”;
从“firebase”导入firebase;
const GoogleAuth=()=>{
const[user,setUser]=useState({});
const[isSignedIn,setIsSignedIn]=useState(null);
var provider=new firebase.auth.GoogleAuthProvider();
provider.addScope('https://www.googleapis.com/auth/userinfo.email');
firebase.auth().useDeviceLanguage();
firebase.auth().onAuthStateChanged(函数(用户){
如果(用户){
setIsSignedIn(真);
}否则{
setIsSignedIn(假)
}
});
常量登录=()=>{
firebase.auth().signInWithPopup(提供程序)。然后(函数(结果){
//已登录用户信息。
setUser(result.user);
setIsSignedIn(真);
}).catch(函数(错误){
console.log(错误);
});
}
常量注销=()=>{
firebase.auth().signOut().then(函数(){
console.log(“注销成功”);
setIsSignedIn(假);
}).catch(函数(错误){
console.log('logout error');
});
}
常量渲染按钮=()=>{
if(isSignedIn==null){
返回(
);
}否则如果(isSignedIn){
返回(
签到
);
}否则{
返回(
签名
);
}
}
返回(
{renderButton()}
);
};

导出默认GoogleAuth假设您的result.user是一个对象,现在将其设置为

setUser({...result.user});

在某些情况下,当直接设置状态不更新时,它对我有效。

您是否确认
结果
参数具有您想要设置的值?使用console.log将其签出。是否使用console.log检查状态是否已设置?如果是,那么它可能显示状态,也可能不显示状态,因为它是一个异步进程。您可以使用useEffect Hook检查状态是否已更新。您可以添加console.log(结果)时看到的内容吗?我发现了问题。正如@harmandepsingkalsi所述,登录是一个异步过程。因此状态没有立即更新,
console.log(user)
给出了以前的状态。我使用
useffect()
来检查和控制台日志,如果用户状态发生了变化,那么它确实会发生变化。
setState
是async process@Saran Raj,只是为了更正。