Answer the question
In order to leave comments, you need to log in
How to refer to a context that does not have a name?
Trying to study the topic of hooks, I ran into a misunderstanding. When I used hoc, I declared the context like this:
const {
Provider: CompaniesStoreServiceProvider,
Consumer: CompaniesStoreServiceConsumer
} = React.createContext();
const myContext = React.createContext();
Answer the question
In order to leave comments, you need to log in
No.
In useContext() , you need to pass a reference to the context object that you did not save. In js, undeclared objects cannot be accessed. Moreover, as soon as the object reference is not used, the object itself is removed by the garbage collector.
const MyContext = React.createContext();
const {
Provider: CompaniesStoreServiceProvider,
Consumer: CompaniesStoreServiceConsumer
} = MyContext;
useContext(MyContext);
Export the variables CompaniesStoreServiceProvider, CompaniesStoreServiceConsumer. You wrap the application in a Provider, then wrap it in a Consumer wherever you need to and get the data from the props
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question