I am trying to get started with React context hooks and I seem to be having an issue I dont understand.
I have defined a user context that is simply a string that says "hello user" as such:
import { useContext, createContext } from "react"
export const UserContext = createContext<string | null>(null)
export interface IAuth {
children: React.ReactNode;
}
const Auth: React.FC<IAuth> = (props) => {
return(
<UserContext.Provider value={"hello user"}>
{props.children}
</UserContext.Provider>
)
}
export default Auth
Then I am attempting to access is like this:
const Dash: NextPage<dashPageProps> = (props) => {
const contextMsg = useContext(UserContext)
const submitForm = () => {
console.log("logout")
}
return (
<Auth>
<div className="w-screen">
<div className='text-xl'>
Dashboard
</div>
<h1>{contextMsg}</h1>
<button className='bg-gray-400 border-2 border-gray-600 w-fit mt-5' onClick={submitForm} >Log out</button>
</div>
</Auth>
)
}
export default Dash
But nothing is being printed out even though I have set a value when using UserContext.Provider
. It will have a value if I specify one when creating the context but not when I set it through the provider.
What am I doing wrong here?