I am trying to update a context with a callback. When I execute the callback its fine, but when I try to update the property inside of it im getting the err Error: Invalid hook call. Im not very familiar with context and researched and cant figure it out. Any help would be great. Thank you.
const initialState = {
language:"en",
showSomething:false,
setShowSomething:setShowSomething
}
const StateContext = createContext(initialState)
export const useGlContext = () => {
const glContext = useContext(StateContext)
return glContext
}
function setShowSomething(el) {
const context = useGlContext()
context.showSomething=el
}
function App() {
const context = useContext(StateContext)
const { language, showSomething, setShowSomething } = context
return (
<Child
language={language}
showSomething={showSomething}
setShowSomething={setShowSomething}
/>
)
const Child = ({language, showSomething, setShowSomething }) => {
return (
<button onClick={() => setShowSomething(true)}></button>
)
}
}
Edit:
I am wrapping the root component inside Provider:
<StateContext.Provider value={initialState}>
<App />
</StateContext.Provider>