React Context
Tags: react, beginners
Published: 2023-12-08 22:36:00

What is Context and when should it be used?

**What is Context**

When you first learn about React, you learn about state and props and how they are used to allow data to flow from one component to another. But what if you need to pass the same data to components that aren't parent and child to one another? Thats where Context comes in. 
Context allows us to share data "globally" between components without the need to pass props. Essentially, sharing data just got easier.
Context has _createContext_ that helps create a context object, and _useContext_ is the hook that allows the data to be passed.
Context avoids the need for prop-drilling - the passing down of props through multiple levels of components that don't need the prop, just to get it to a deeper nested component that does need it.

![Example of theme prop](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ugbjy4kg2r0ok0gyre71.png)

So, with our example above, we see that the Interests component needs the theme, but it's parent doesn't. With prop-drilling the theme prop is passed down from the App to the Profile(where it isn't used), and then passed straight down to the Interests component.

**When Should Context Be Used**

Context should be used when an element needs to be used globally. The data shouldn't need to be updated very often either.
Examples of things that should be placed in Context:
 - Language
 - Themes
 - User data (current authenticated user) 
 - Audio (a music app)

**How Do You Use Context**

The first step is to create context. Here is an example from a music app I was working on.


![createContext](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jh0pu6mt0y0lrvzx6csj.png)

This created a context object called MusicContext. Context data have two components the context object and the provider. Before I move on to showing the provider, I want to show that in my music app, I went ahead and called my context hook useContext.  It is paired with my context object and looks like this:

![useContext](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ajh5r7ci7qq9xmzybmwd.png)
You don't have to do it that way, you can wait and use the hook in the components where you need to call the context. The way I did it means that I just need to call my useMusicContext = value. The Value is declared by the provider 👇🏻.


Here is an example of what that same music app's provider looked like:


![ContextProvider](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vnk9nq3zk5nqjz70ne2e.png) 

The provider gives access of the context data to the child components.

Here is what the component looks like all together, including it's export variables.


![MusicContext.js component](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ki5v4rv3x467m9udqmqp.png)

Now that I have my Context, what do I do?

The next step is to wrap the Provider component around whatever component children will need to access the data. This usually occurs in one of the top most levels in your component hierarchy.

Using another example, where a UserContext and UserProvider had been created, the App component imported the UserProvider and wrapped it around the Header and Profile components, to pass down the theme.


![UserContext.Provider](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zrdffipopkfq3ww5o1au.png)
![Provider wrap](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dsksqvn24w93miinzf11.png)
In this example the value being passed to Header and Profile is [user, setUser]. When Header calls the context this is what it looks like: 


![Header calling useContext hook](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2a48lm1kyr03506rga61.png)
 
The Profile component looks similar: 


![Profile calling useContext hook](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/phfxe0xmucuaxsd4mx4z.png)

So recap our steps:
 1.) We created a context using the createContext method.
 2.) We then took that created context and created a provider to wrap around our components.
 3.) In the that provider we created and defined a value prop that could be passed down.
4.) Lastly we called that value within our components, that had previously been wrapped by the provider, using the useContext hook.

The end result of the UserContext example is:


![Header and Profile](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x28khdjy90qercvdsmby.png)

![Header Profile and Interests](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fo4tt67l6nnt8tjp4xz9.png)

As for the Music Player:


![Music plays on Explore](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9qm3atieutx0v32cs158.png)


![Music plays on Home](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vl2z7dkw03a5xuvi6s6w.png)

The MusicPlayer was able to continuously play the song, uninterrupted, as I moved from the Explore page to the Home page. 


_______________________________________________
Sources:
Barger, Reed. "React Context for Beginners – The Complete Guide (2021)". Posted 21 July 2021. Retrieved from https://www.freecodecamp.org/news/react-context-for-beginners/ on 6 December 2023.
"React Context". Flatiron School/GitHub. Retrieved from https://learning.flatironschool.com/courses/7022/pages/react-context?module_item_id=624604 and https://github.com/learn-co-curriculum/react-hooks-use-context November 2023.