<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Pulkit Sharma on Medium]]></title>
        <description><![CDATA[Stories by Pulkit Sharma on Medium]]></description>
        <link>https://medium.com/@pulkit.sharma_48143?source=rss-c215d926e9e9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ZdVo_K8A7ssRGIO8PWtBHA.png</url>
            <title>Stories by Pulkit Sharma on Medium</title>
            <link>https://medium.com/@pulkit.sharma_48143?source=rss-c215d926e9e9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 14:59:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pulkit.sharma_48143/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[React Context API : A complete guide]]></title>
            <link>https://medium.com/vectoscalar/react-context-api-a-complete-guide-bc0898b71dc9?source=rss-c215d926e9e9------2</link>
            <guid isPermaLink="false">https://medium.com/p/bc0898b71dc9</guid>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[context-api]]></category>
            <category><![CDATA[hooks]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Pulkit Sharma]]></dc:creator>
            <pubDate>Mon, 06 Sep 2021 13:38:05 GMT</pubDate>
            <atom:updated>2022-02-12T12:49:10.896Z</atom:updated>
            <content:encoded><![CDATA[<h3>React Context API : A complete guide</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*klQsetQ8k489HOtt" /><figcaption>Photo by <a href="https://unsplash.com/@flowforfrank?utm_source=medium&amp;utm_medium=referral">Ferenc Almasi</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>If you are working on or learning React JS then you must have heard of the States. These States represent an object that holds some information that may change over the lifetime of the component. In other words, these States are nothing but a way to create or dictate how your user interface will look like.</p><p>In this article, we will talk about how we can handle these states using Context API which is available after React version 16.3. But, before jumping on that, let’s see why there is a need of such thing in first place. Also, we will be using functional components and hooks for code demonstration.</p><p><strong>Without Context API</strong></p><p>One of the major advantages of using React is you can break your application into smaller components (as per the need) for better reusability &amp; maintainability of code. And, as the size of the project increases, these components will likely to grow in numbers.</p><p>Now, let’s assume if a user is interacting with one component somewhere on the UI and that information or input is required somewhere else inside a different component. What we can do? Yes, you are thinking in the right direction. We can pass a prop to that component. Then, what is the problem?</p><p>Let’s assume you have 10 components in your component hierarchy and the very first component updates something which is required by the last component (10th one). In that case, you have to pass the prop down the whole hierarchy so that it is available for the 10th component. This is where we can slow down our application and cause development issues.</p><p>This is what we call “prop-drilling.” This can slow down your development as the size of your application grows (assuming the number of components also grows).</p><p>This is one of the main reason why Context API comes into play.</p><p><strong>With Context API</strong></p><p>Context API provides a way to pass data between your components effectively without passing props at each level. Generally, we create these contexts to store information which is labelled as “global” i.e., which are required at more than 1 place. For example, theme information, user authentication, language etc.</p><p>Let’s take a simple example of a calculator. For simplicity, we will perform addition, subtraction, multiplication &amp; division.</p><p>First, we will create a context file which will perform above operations.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9be47d25bc1901b05cf05fc7a6f4b975/href">https://medium.com/media/9be47d25bc1901b05cf05fc7a6f4b975/href</a></iframe><p>After that, we will create few more files which will help us to perform above mentioned operations like addition, subtraction etc by consuming our calculator context. Let’s see how these files will look like.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0c2321f9b720c498eecd2310b0fa6c09/href">https://medium.com/media/0c2321f9b720c498eecd2310b0fa6c09/href</a></iframe><p>Similarly, we can create files for other operations as well. After that, our App. js file should look something like this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6fbdf6dcdcc1fbec59e2b6ae514d266a/href">https://medium.com/media/6fbdf6dcdcc1fbec59e2b6ae514d266a/href</a></iframe><p><strong>Performance Impact</strong></p><p>So far we have seen how we can create &amp; consume a context. But, what about the performance of the Application. Will it become better or go down. Unfortunately, it’s the latter one.</p><p>Let’s check the rendering of UI components whenever we perform some operation. For that we will put a simple console.log() in each of the components where context is consumed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*Mb49wAJeuMF4aTCpfJDwqw.png" /></figure><p>Above is the output if we perform the addition. Clicking on any other operation yields the same output in a different order (consoles). You can clearly see each of the components which consumes calculator context is re rendering.</p><p>Maybe it doesn’t seem like a big issue. Maybe it can work for a smaller application like ours, but think of a bigger Application which has hundreds of components which consumes a context. So, the next question is …</p><p><strong>When and how to use Context?</strong></p><p>If the state updates are frequent then you should avoid using Context and look for other solutions like React Redux or MobX-State-Tree.</p><p>But, if you have a state which remains same for most of the times like theme, preferred language or authentication information then React Context is a good solution.</p><p>But, if you are still inclined towards using the Context API, then try to split the bigger contexts into the smaller multiple contexts. This will lower down the unnecessary re rendering till some extent and you will be able to use React context which can be really good for smaller and simpler applications.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bc0898b71dc9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/vectoscalar/react-context-api-a-complete-guide-bc0898b71dc9">React Context API : A complete guide</a> was originally published in <a href="https://medium.com/vectoscalar">VectoScalar</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>