arnaudambro
arnaudambro

Reputation: 2673

Can we create multiple contexts in React Context API?

Thanks to @wesbos I have learned how to use the new React Context API, and I am already using it in my projects, that's great (here it is for the lesson). As I saw that in order to create a context, we need to do :

const MyFirstContext = React.createContext()

I was wondering if it was possible to create another context in the same App. For example I am creating a game which would need its own game context, and the game is played by users, each user having its own informations, so each user would need its own user context. So I did :

const MyGameContext = React.createContext();
const UserInfoContext = React.createContext();

But it's not working, so I suppose it's not supposed to work that way. How should I proceed ?

[EDIT] I clarified a bit my question because I thought it was not crystal clear... and I rectified it, because I made a mistake in it : I didn't understand that what was inside the parenthesis was the default value of the context.

Upvotes: 4

Views: 5527

Answers (1)

arnaudambro
arnaudambro

Reputation: 2673

I should have waited a bit for React's documentation about Context to be updated. It seems that I must have made a mistake in my code, because creating multiple contexts is as simple as this:

MyFirstContext = React.createContext();
MySecondContext = React.createContext();
...

Upvotes: 4

Related Questions