I have been looking around, but can't find a definite answer. I have wrapped my app in <AuthContext>
, I have passed props and tried return <div>{props.user.username}</div>
but nothing works, just getting the same TypeError: Cannot read property 'username' of undefined
over and over again. Am I doing something fundamentally wrong here or is there a simple answer to this?
import React, { useContext } from "react";
import { AuthContext } from "../../context/auth";
const HeaderBar = (props) => {
const {user: { user }} = useContext(AuthContext);
if (user) {
console.log(user.username); // Returns user in console log, so everything seems to be working fine
}
return <div>{user.username}</div> // Throws an error "TypeError: Cannot read property 'username' of undefined"
}
export default HeaderBar