State management in React application using Context API
Tags: frontenddev, statemanagement, react, reactcontextapi
Published: 2020-01-12 14:38:19

---
title: State management in React application using Context API
published: true
date: 2019-12-10 11:57:04 UTC
tags: front-end-developmen,state-management,react,react-context-api
canonical_url: 
---

![](https://cdn-images-1.medium.com/max/1024/1*Ewk8QbNnNKVFzIXAtsvC5w.png)

Before we start, visit this demo application. This will give you a rough idea of what we are going to discuss in this article.

[React App](https://rishikeshvedpathak.github.io/react-context-example/)

If you are one of the developers who is using redux to manage you application’s state then you must be very curious to know more about how can I use the react’s new Context API to manage the my application’s state.

You might have the same set of questions in your mind as I have,

1. What is React Context API?
2. Does it serve the same purpose as redux?
3. Will it replace redux? and does that mean I can get rid of the complexity of redux? 😜
4. Is it the future of state management?

Well… I might help you to find answers for these. I have written a simple application to demonstrate how we can use React’s Context API for state management. Below is the repo link,

[RishikeshVedpathak/react-context-example](https://github.com/RishikeshVedpathak/react-context-example)

#### What is React Context API? And when to use it?

> Context provides a way to pass data through the component tree without having to pass props down manually at every level.

When we start writing a react application, we need to pass some data between components. Typically, even before using redux, we pass data top-down (parent to child) via props. Which woks fine in initial stages, but as your application scales it becomes cumbersome for us to pass the data in this way. Context API provides an alternative way to share values like these between components without having to explicitly pass a prop through every level of the tree.

In [**_React v16.3.0_**](https://reactjs.org/blog/2018/03/29/react-v-16-3.html) context api was introduced. Before that, we used to use redux to handle this situation.

#### Why Context API and not props?

It is true that you can handle everything with props which you can with Context API, but then why to use Context API?

Consider your application structure where you have multiple components defined. And these components may have parent-child relation or they might be siblings. In either case wouldn’t it be cumbersome for you to pass data down the tree. What if there is way you can manage the data at a single location(store) and this data is then be used by all the components. This sounds nice, right!!

This is what we can achieve using Context API. See the below image for better understanding.

![](https://cdn-images-1.medium.com/max/785/1*gLC1UP2zOf3FKcJwYBO6Gw.png)<figcaption>Props Drilling vs Context API</figcaption>

#### Let’s see it in action

See the example here,

[React App](https://rishikeshvedpathak.github.io/react-context-example/)

{% youtube 8PkQi7dq0U4 %}

This application consists of components,

- Header
- Home
- Cart

These three components share the same data. Any event taken from these components will update the same data.

1. Create a context(store) for your application.
<iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/85688c7960d37d35afe87117a5abad4e/href">https://medium.com/media/85688c7960d37d35afe87117a5abad4e/href</a></iframe>

2. Tell your application to use this context.

<iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2fcb454dc036bb26e56eed9906f9a4d7/href">https://medium.com/media/2fcb454dc036bb26e56eed9906f9a4d7/href</a></iframe>

3. Read data from Context

This is very simple. Use just need to use react’s new hook called **useContext**

**const cartContext = useContext(AppContext)**

The cartContext will now hold the data for your component.

4. Update data in Context

<iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cfd768cddf71173d52aee2ef1fc52660/href">https://medium.com/media/cfd768cddf71173d52aee2ef1fc52660/href</a></iframe>

The function **handleCartUpdate** will update the data in context. This will the be reflected in header as well as in Cart component.

#### Conclusion

We have successfully created a simple react application to demonstrate the use case of Context API. You can find the final code in the GitHub repo,

[RishikeshVedpathak/react-context-example](https://github.com/RishikeshVedpathak/react-context-example)

I hope this will help you to understand and encourage you to freely use React Context API in your application. Please feel free to give feedback and suggestions!