React useContext
up:: React Hooks
Context
State chains
- If you pass a state around a lot, use context
- Or if you use a prop only to forward props but not do anything with it
- You want to avoid a lengthy ‘prop chain’
Creating a context
store/
is a common folder name
store/auth-context.js
(kebab- instead of CamelCase because it’s not a component)
You can even add functions in context!
Providing context
Wrap it around all the elements that need it.
For example, in app.js
(this makes it available to all)
Listening to context
Consumer (is kinda meh as a solution)
Easier solution
React useContext