<?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 Susan Joby on Medium]]></title>
        <description><![CDATA[Stories by Susan Joby on Medium]]></description>
        <link>https://medium.com/@suz.jo92?source=rss-f8e07308e798------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*paqsCnmWatiYmTQAGFICSg.jpeg</url>
            <title>Stories by Susan Joby on Medium</title>
            <link>https://medium.com/@suz.jo92?source=rss-f8e07308e798------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 15:18:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@suz.jo92/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[Context System in React]]></title>
            <link>https://medium.com/@suz.jo92/whats-new-in-web-design-14727a916767?source=rss-f8e07308e798------2</link>
            <guid isPermaLink="false">https://medium.com/p/14727a916767</guid>
            <dc:creator><![CDATA[Susan Joby]]></dc:creator>
            <pubDate>Fri, 08 Feb 2019 18:17:12 GMT</pubDate>
            <atom:updated>2019-02-13T13:28:36.210Z</atom:updated>
            <content:encoded><![CDATA[<p>React Library enables flow of data in an application with state and props. Components in a React application share data in a component tree using the concept of <em>‘prop drilling’</em>. Prop drilling is a concept where data is passed from a component in the higher level(parent) to a component in the lower level(child), in component hierarchy.</p><p>Well folks, if you are familiar with React applications, you already know the good and bad sides of prop drilling. It can be quite a task when you have to pass down data to a deeply nested child component. And yes, the ‘Context’ system in React aims to resolve this very problem.</p><p>Let’s compare the traditional parent-child method of passing props to Context API in React.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/393/1*IRFAyFRKcRVV75zNEe3GaA.png" /><figcaption>Passing data from parent to child</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/399/1*gxfVr37eJUXeFmlvaMK1yA.png" /><figcaption>Passing data using Context</figcaption></figure><h4>Why use Context in an application</h4><p>Context is useful if you need to pass data to several deeply nested elements in your application. It eliminates issues with prop drilling.</p><p><em>Context is designed to share data that can be considered “global” for a tree of React components, such as the current authenticated user, theme, or preferred language.</em></p><h4>How to use Context in an application</h4><p>Let’s look at a simple example to understand the concept of Context.</p><p>Here’s a ContactInfo App which displays a list of names and contact details.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*oUorwBn5W3P9nycL09BJsA.png" /><figcaption>Component hierarchy in ContactInfo Application</figcaption></figure><p>Here, you can see three components, ContactDetails, Address and ContactButton.</p><ol><li>ContactDetails accepts a username</li><li>Address and ContactButton receive contact details of selected user from ContactDetails.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*-AzJDxIuFT8xyLNP9ZcM8A.png" /><figcaption>ContactInfo App with list of users and details of selected user John.</figcaption></figure><p>Now let’s see what the final code looks like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/923/1*IT32j6cAPswbualqCq7msQ.png" /><figcaption>Render method in App.js</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dwWmtWBqs_ZsCJfvDx1f9Q.png" /><figcaption>ContactDetails.js rendering Address and ContactButton</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/896/1*400bWUiqmHYWNOeuD2MuZg.png" /><figcaption>Address.js</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/825/1*JvGShcheha36xnW_vdzpzQ.png" /><figcaption>ContactButton.js</figcaption></figure><h4>Create Context in an application</h4><p>To create a Context, use the following command.</p><pre>const newContext = React.createContext(defaultValue)</pre><p>Let’s create UserContext.js.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/877/1*tJexbtRN7Q7Ouso8WZgzmQ.png" /><figcaption>UserContext.js</figcaption></figure><h4>Setting Default Context Value</h4><p>Context can take values of any type including string, numeric, object or array. Use the following command to set the value ‘John’ to UserContext .</p><pre>const UserContext = React.createContext(&#39;John&#39;)</pre><h4>Consume a Context</h4><p>To consume a context in a class, we need to establish a relationship between the the class and the context object.</p><pre>static contextType = NewContext</pre><p>Now that connection is established, we can access the context object using ‘this.context’.</p><pre>const user = this.context</pre><h4>Context Provider</h4><p>React applications exploit state and props to subscribe to changes. Similarly, any component that consumes the context can subscribe to the changes using ‘Provider’.</p><pre>&lt;NewContext.Provider value={value}&gt;<br>&lt;/NewContext.Provider&gt;</pre><p>In our ContactInfo App, the components Address and Contact Button consume user details. We have user details of the selected user in a container, and both Address and Contact Button can access it.</p><p>So let’s put it this way. We wrap ContactDetails with ‘UserContext.Provider’ and set the details of selected user to the Context. Now the user details are available to both Address and ContactButton as props, without the need of prop drilling.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/913/1*NOYo3ldF8rt1mrjT-KQdiw.png" /><figcaption>Provider to set value of selected user to context object.</figcaption></figure><h4>Context Consumer</h4><p>Context Consumer lets you subscribe to context changes and manipulate the same using a function.</p><pre>&lt;NewContext.Consumer&gt;<br>  {value =&gt; /* manipulate data based on the context value */}<br>&lt;/NewContext.Consumer&gt;</pre><p>In ContactInfo app, we use UserContext.Consumer to perform null check on contactNo and display the value in Contact button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bl-YUqVBvv31mcD4fBJycQ.png" /></figure><h4>Conclusion</h4><p>All said and done, Context in React just focuses on passing data to nested components in an app, without having to pass down data level by level. It is never an alternative to state management libraries like Redux or MobX.</p><p>For more information on the usage of Context in React, please refer the React docs at <a href="https://reactjs.org/docs/context.html">https://reactjs.org/docs/context.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=14727a916767" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>