How to fix React Context's object is not a function - TypeError

5.5k views Asked by At

I am trying to solve this error for a few days now, I'm trying to pass context to my components, whatever I try, I get the error "The object is not a function" or "The object is not iterable"

I was trying to do this with Typescript, and I thought it might be something else, so I tried using react javascript cli and here are the errors below with my code pictures.

Error

Wrapper

Context

Component

1

There are 1 answers

1
Adam Jenkins On

The value of the context is {state,setState} not [state,setState]

Just do this:

console.log(useContext(GlobalContext))

and you will see that the context is an object with two properties - state and setState as given by this line:

<GlobalContext.Provider value={{state,setState}}>

Also, post code, don't post pictures of code.