I'm getting the error "Cannot destructure property 'currentUser' of 'Object(...)(...)' as it is null" when I use useContext() hook from react in Next.js.
// Other imports
import CurrentUserContext from "../../contexts/current-user/current-user.context";
class Layout extends React.Component {
constructor() {
super();
this.state = {
currentUser: null,
};
}
unsubscribeFromAuth = null;
componentDidMount() {
// Auth code
}
componentWillUnmount() {
// unsubscribe code
}
render() {
const { children, title } = this.props;
return (
<React.Fragment>
<Head>
<title>{title}</title>
</Head>
<CurrentUserContext.Provider value={this.state.currentUser}>
<Header />
</CurrentUserContext.Provider>
{children}
<Footer />
</React.Fragment>
);
}
}
export default Layout;
Here is where the error happens
// Other imports
import CurrentUserContext from "../../contexts/current-user/current-user.context";
import { useContext } from "react";
const Header = () => {
const { currentUser } = useContext(CurrentUserContext);
return (
// Header Jsx
);
};
export default Header;
This is how I created the context file
import { createContext } from "react";
const CurrentUserContext = createContext(undefined);
export default CurrentUserContext;
Any help is appreciated.