I have this function:
let userDisplayName;
firebase.auth().onAuthStateChanged(function(user){
if (user){
userDisplayName = user;
}
else {
console.log('error');
}
this.setState ({user: userDisplayName})
return userDisplayName;
})
console.log(userDisplayName, 'udn');
userDisplayName
is giving me the correct value however, only in the scope of the function. When I console.log
I am told that the value is undefined and when I try to setState
I get told I cant use that function inside there (doesn't know what this
is). How can I return the variable so that it has the value I'm expecting outside the scope so that I can set state with it?