<?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 Shobhit Ghai on Medium]]></title>
        <description><![CDATA[Stories by Shobhit Ghai on Medium]]></description>
        <link>https://medium.com/@mail.shobhitghai?source=rss-db0b22411dea------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*usfNHzrpcwEFVe6QULq9_g.png</url>
            <title>Stories by Shobhit Ghai on Medium</title>
            <link>https://medium.com/@mail.shobhitghai?source=rss-db0b22411dea------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 03:10:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mail.shobhitghai/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]]></title>
            <link>https://medium.com/@mail.shobhitghai/react-context-api-2344dd784772?source=rss-db0b22411dea------2</link>
            <guid isPermaLink="false">https://medium.com/p/2344dd784772</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Shobhit Ghai]]></dc:creator>
            <pubDate>Wed, 16 May 2018 11:50:11 GMT</pubDate>
            <atom:updated>2018-05-16T11:58:23.952Z</atom:updated>
            <content:encoded><![CDATA[<h4>An overview of how to use new Context API and can it be a replacement for redux or mobX.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*I-hbpBllWxJjxeZn." /></figure><p>Last year at a react meet-up there was a discussion on why react is not providing any built in solution for application level state management. The answer which most of us agreed at that point was not to diverge from principal from just a view layer that can sit on top of any other framework if needed. Though react was having “context API” but it was a experimental feature, which means its a risk using it for a production ready application.</p><p>But redux/mobx were using the same experimental context api, using the built in react functions and create helper functions/wrapper to provide a state management. And they are compatible with any framework and not just bound to react.</p><p>Now with 16.3, react has introduced (officially) the <a href="https://reactjs.org/docs/context.html">Context api</a> that can be used as a built in state management rather than using the inbound component level state solution or any external state management library(at least for smaller application).</p><blockquote>Context provides a way to pass data through the component tree without having to pass props down manually at every level.</blockquote><p>In a typical standalone react application data will be passed from parent components to child components via props, but as the child components grows in number you have to pass down the data from each layer of nested children to reach out to the intended components. This is called as <strong>props drilling</strong>, and the problem worsens when the child components is responsible to calling a function at parent level. Eg. callbacks in button component to submit a form at a parent form component which is 3–4 layers above that or maybe configuring your application for multiple themes where the selected theme needs to be passed as every levels of your components for different styling.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3tSxoLn3mM-8wb0UN-_bGQ.png" /><figcaption>Props drilling and passing callbacks to child components</figcaption></figure><p>To overcome this design pattern Context API provides a way to share states or function like this between components without having to explicitly pass a prop through every level of the tree. Using context api in your application you can avoid passing props through your intermediate components. Lets dig into some code for details of what exactly is Context API and how it provides and consumes the shared state within the application.</p><p>We will be creating a small application with a login form and on submission navigate to home page to fetch user data, the state across application will be managed with React Context API.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2F7rmwvl3w1%3Fview%3Dpreview&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2F7rmwvl3w1%3Fview%3Dpreview&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2F7rmwvl3w1%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/a96b54ae2f1fd898be7f635f2c9f669c/href">https://medium.com/media/a96b54ae2f1fd898be7f635f2c9f669c/href</a></iframe><h4><strong>React.createContext</strong></h4><p>Lets create our context store with default values.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4d6ad10a32cfea439c5af2a073e035f5/href">https://medium.com/media/4d6ad10a32cfea439c5af2a073e035f5/href</a></iframe><p>The context object we created above will expose <strong>Provider and Consumer</strong>. As suggested by the name Provider is responsible for providing and updating the state. And consumer is responsible for subscribing to the updates from the provider its listening to.</p><p>Now lets code our provider for managing login state, validation and fetching user data, this can also be consider as <strong>“actions”</strong> file in flux/redux terminology.</p><p><strong>Provider:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d233f85b501eab29e13ef3f7215097ed/href">https://medium.com/media/d233f85b501eab29e13ef3f7215097ed/href</a></iframe><p>Ok so now we have our store with default values and a provider to interact with our components and providing updates. In order to consumer the updates lets quickly write a consumer.</p><p><strong>Consumer:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/986c602e2e93f8f7838c5dcb0710951d/href">https://medium.com/media/986c602e2e93f8f7838c5dcb0710951d/href</a></iframe><p>Note our login page is calling onEmailChange, onPasswordChange and onLogin actions from the provider we exposed via context api. And we haven’t really passed any of these as props from parent component.</p><p>As now you can see its a pub-sub model where as consumer is subscribing to a provider. But before that we need to specify this connection as in our application we can have multiple providers and we need to tell consumer which provider it needs to be listening to.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ceaba82dee20632ab4bfa9329d366e5c/href">https://medium.com/media/ceaba82dee20632ab4bfa9329d366e5c/href</a></iframe><p>For any consumer to subscribing to a provider we need to pass it as a children to that provider within Consumer block.</p><p>In a similar way lets hook up our homepage with the provider we wrote earlier to fetch the user data after logging in. Note in order to access provider actions from the lifecycle method we need to wrap our component and explicitly pass the context (workaround/hack? Lets hope it to be fixed in next update)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/62698c42ec6be4d0960ef66ae4b3c5c5/href">https://medium.com/media/62698c42ec6be4d0960ef66ae4b3c5c5/href</a></iframe><p>You can play around with the application we just code from the link below, it has context, provider and consumer files all stitched together.</p><p><a href="https://codesandbox.io/embed/7rmwvl3w1">CodeSandbox</a></p><p><strong>Is this end of Redux?</strong></p><p>Ok, we just saw Context API and its awesome capabilities but is it really a replacement for state management libraries like Redux or mobX. Lets see.</p><p>Context api is no longer a experimental feature, means you will be safe to implement in your production ready application and will be of great use for sharing data between independent components. But it does not provide you anything more than this. With Redux you get Dev tools to trace your application state, middleware to intercept and add your centralised logic, redux-thunk for action creators. Apart from these you also have other libraries on top of redux like redux-persist to handle data in localStorage.</p><p><strong>So using context Api or using any state management library really depends on the complexity and needs of your application</strong>. For a smaller application where the ask is just to have a centralised place for storing your application state where the components can easily access it without falling into props drilling then Context API is your solution. Anything above these requirement its best to have a state management library. Another thing to note here is React Redux or mobX uses context internally but it doesn’t expose this fact in the public API. So, in short its far too soon to say that Redux will be replaced by the new Context API.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2344dd784772" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>