React Context in Vue
Tags: vuex, react, vue, javascript
Published: 2019-11-11 12:10:04

---
title: React Context in Vue
published: true
date: 2019-10-31 23:05:29 UTC
tags: vuex,react,vuejs,javascript
canonical_url: https://medium.com/@alirezavalizade/react-context-in-vue-d40ee145974d
---

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

#### This article is about switching to Vue.js with Context mindset.

I think these days every developer have heard about the context in React.js, So I’ll straightly go over the code and the solution. If you’re not familiar with Reacts context [click on the link](https://reactjs.org/docs/context.html).

### Provide And Inject

If you are a React developer and thinking like React “context” you cannot work with Vue.js without the Provide and Inject feature (I mean it’s hard, really).

Let’s see a simple example of Provide and Inject:

![](https://cdn-images-1.medium.com/max/1024/1*516MhQFFRZJsGFO-c3w5AA.png)<figcaption>A simple example of Provide and Inject</figcaption>

So basically “Provide” it’s for passing data or methods to the children with “Inject” property, regardless of how deep they are, But what will be solved exactly?

1. Not using props to pass the data to children like 5 levels
2. Clean, simple and powerful store management and not using the Vuex when it’s not necessary
3. DRY

### Vue source code

These two screenshots are part of provide and injection in Vue source code (after normalisation them).

- Provide: You can write it as Object or a function which returns an Object which you can access to the component context (this) and you can pass method or computed properties or whatever in the component.
- Inject: You can write it as an Array of strings or Object with a default value.
- about resolveInjection function: It’s a simple loop over the inject object and looking for the provider with a “while” loop to resolve it.

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

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

### Simple store management with Provide and Inject

We’ll build the “famous theme handler” with context to understand how easy and useful is the context.

1. [Use Vue cli to bootstrap your project](https://cli.vuejs.org/).
2. Now, You can have a structure like this.

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

As you see we have a weird “context” folder which we’ll look into it in further steps. :)

3. Create a Javascript file in “styles” folder and call it “themes.js” with these contents.

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

4. Let’s go to “contexts” folder, Create a JS file and call it “Theme.js”.

- We have two components in the same file with the string templates in this case you need the standalone build, [here is a helpful section explaining](https://vuejs.org/v2/guide/installation.html#Runtime-Compiler-vs-Runtime-only) (of course you can have separate files like ThemeProvider.vue and ThemeConsumer.vue).
- First one is our Provider which provides the data and methods to injectors.

![](https://cdn-images-1.medium.com/max/1024/1*HTEVcBOJH8XMEZtQLm8nUA.png)<figcaption>Theme.js</figcaption>

- Second one is our Injector (Consumer) which is able to inject the provided data. actually second one is just a helper to write less code and reuse it every where.

![](https://cdn-images-1.medium.com/max/1024/1*oRCLbx14ghFd_CL9eWPZJw.png)<figcaption>Theme.js</figcaption>

And that’s it, we are good to go. You just need to import the Consumer and then you can access to provided data using “slot-scope”.

5. You’ll need to import and wrap the Provider inside your application root. Unfortunately there’s no Hooks in the current versions of Vue yet to use it like React (useContext).

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

6. Let see some magics. We have two components which we’ll see the different use cases.

- First component: We see a component which wrapped with the Consumer and you can access to the data and methods by “slot-scope”. You can bind styles and pass the theme variables or you can bind classes and handle the styles and theme using css!

![](https://cdn-images-1.medium.com/max/1024/1*cPfvoTujOf2bnABgT4w8nQ.png)<figcaption>FirstComp.vue</figcaption>

- Second component: We see a component which wrapped with a “div” in template and includes two ThemeConsumer, this means you can have a lot of Consumers in every where also you can override them easily.

![](https://cdn-images-1.medium.com/max/1024/1*lTQlSbEljZq31U-VLU6THw.png)<figcaption>SecondComp.vue</figcaption>

### Live demo and source code
<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fvue-template-z2twq%3Ffontsize%3D14&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fvue-template-z2twq%3Ffontsize%3D14&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2Fz2twq%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/db4b42c5bcdecd87f5da93b3902772b6/href">https://medium.com/media/db4b42c5bcdecd87f5da93b3902772b6/href</a></iframe>
### Keep in mind

> **Note: the**  **provide and**  **inject bindings are NOT reactive. This is intentional. However, if you pass down an observed object, properties on that object do remain reactive.**

### Summing up

No matter if you are working with React or Vue, “Context is everything”! It’s very simple to understand, divide you application code and structure. It solves the problem of passing stuff deep down your component.

Thanks for reading, If there is anything I have missed, or if there is a better way to do something, then please let me know. 🤙

You can find me on [Github](https://github.com/alirezavalizade) and [Twitter](https://twitter.com/alirezavlizade)!