useContext in React
Tags: webdev, tutorial, beginners, react
Published: 2022-09-07 05:02:00

We use React Context to **manage the state at a global level**. Components can get data from the React context **no matter how far down the components tree they are**.

The very first step used in creating Context is to import “**createContext**”

![](https://cdn-images-1.medium.com/max/2000/0*9ttMjm5NnDyZlBX6.png)

## Context

Context lets the information be passed to the desired component **without dragging it through multiple middle components** and also when the same information is required by various components. It makes the **parent component** let certain information be available to any component in the component tree below it, **without the use of repeated props.**
The disadvantage of only passing props is that, in situations where the user would want two components’ state to change together and to do that the user will have to remove state from both of them, move it to their closest common parent, and then pass it down to them via props. But the closest common parent could be far away from the component, in the tree, that needs data. So **lifting the state up that high** could lead to a condition known as **prop drilling.**

![](https://cdn-images-1.medium.com/max/2000/0*sZrzE5AsKdokTGkw.png)

**createContext returns a context object. Components can read context by passing it to useContext().**

## useContext :

It is a React Hook that lets you read and accept a context.
To import this hook we need to write the following code in the top of the code file :

![](https://cdn-images-1.medium.com/max/2000/0*ySwUeLExxTH9xG-Y.png)

Instead of using **useContext(ThemeContext)** again and again in each component, we define a “**Custom Hook**” i.e. a function that does this for us called **useTheme**

![](https://cdn-images-1.medium.com/max/2000/0*1kY4FVgediTP9gXL.png)

We are now trying to create a simple program which will be able to **change the color of the background** and **the text** upon clicking a button.

The very first thing that we will do is create state variables and initialize the state of the state variable.
Here, we initialize the theme state variable to “**white**”

![](https://cdn-images-1.medium.com/max/2000/0*ngXDswP8zPoH-a9m.png)

In this next piece of code, to **pass context** to the children property, we will **wrap it between the context provider and also pass the values “theme” and “setTheme”**. When the children property is wrapped between the context Provider, that component now will be able to **access the mentioned state variables**.

![](https://cdn-images-1.medium.com/max/2000/0*YUncOA6e2IlbCI-d.png)

The final step would be to **export** the useTheme and ThemeProvider

![](https://cdn-images-1.medium.com/max/2000/0*B4RHVXsy-bTZ6h4X.png)

Now, in the App.js file we import the useTheme to be able to use it further into the program

![](https://cdn-images-1.medium.com/max/2000/0*n_o_fqSqKkNWfl0h.png)

Here, we create two context variables, with names **theme and setTheme**. The names of these variables are **independent** of the names of the variables used in the theme-context.js program.

![](https://cdn-images-1.medium.com/max/2000/0*eYEUgy42yEmxkPLG.png)

The ***getStyle*** function returns a conditional statement where it’s programmed to change the state of the state variable according to the conditions mentioned.

![](https://cdn-images-1.medium.com/max/2210/0*PKnPpC64axycMtlm.png)

Here by using the onClick event handler we update the theme and output the desired result.

## Output :

![](https://cdn-images-1.medium.com/max/2000/0*_gknGv4KekrtnFGL.png)

![](https://cdn-images-1.medium.com/max/2000/0*oNaySPjXAypEeLcy.png)

The link for the entire code used above is given below:
[https://codesandbox.io/s/use-context-i29vb5](https://codesandbox.io/s/use-context-i29vb5)