React-context: how can I useContext in class component

I need to useContext in class component in order to change the values saved in context , how can I go about doing that

I just need to call a function like
but its giving error in react class component

Did you check the docs? It has some examples with class components.

I don’t think I have use Context with class components (or maybe I just don’t remember it).

yes I checked that but I’m not really getting how can I use it

Well, I only know what the docs say.

Import the context and set it on the contextType property of the class component, or use a static class field. Then access it using this.context.

ooh I cannot find the import statement used for import context
this one ?
import {createContext} from 'react'

It’s your own context object, the one you created. Just like with the useContext hook.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.