I am trying to retrieve the properties of my firebase Auth object. When I print the Auth object in the console I am able to access the "displayName" property but when I pass the Auth object into my component, I keep getting the error "Cannot read property 'displayName' of null". Here is the code below:
class App extends React.Component {
constructor() {
super();
this.state = {
currentUser: null,
};
}
unsubscribeFromAuth = null;
componentDidMount() {
this.unsubscribeFromAuth = auth.onAuthStateChanged((user) => {
this.setState({ currentUser: user });
console.log(user.displayName); // I am able to access the property here
});
}
componentWillUnmount() {
this.unsubscribeFromAuth();
}
render() {
const { currentUser } = this.state;
console.log(currentUser.displayName); // I am unable to access it here
return (
<div className="App">
<Navbar currentUser={currentUser} />
<Switch>
<Route exact path="/" component={HomePage} />
<Route path="/login" component={LoginPage} />
<Route path="/signup" component={SignUpPage} />
<Route path="/shop" component={ShopPage} />
</Switch>
</div>
);
}
}
export default App;