<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged context-api</title>
<link>https://coderlegion.com/tag/context-api</link>
<description>Powered by Question2Answer</description>
<item>
<title> From Props to Context: Simplifying State Management in React </title>
<link>https://coderlegion.com/386/from-props-to-context-simplifying-state-management-in-react</link>
<description>&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Hey guys . Are u tired of passing of endless props and state variables to the child components while creating an application in React ? Well this post is just your ultimate saviour from that chaos. In this post I'll devlve into the uses of &lt;code&gt;useContext&lt;/code&gt; api in your react applications, which makes your work not only faster but a tad bit easy.&lt;/p&gt;
&lt;p&gt;Before diving into the details, I want to give you a visual aid that will act as the cherry on top of this blog! &lt;/p&gt;
&lt;p&gt;Okay, so first, imagine a store in your locality.  Now, this store in &lt;code&gt;useContext&lt;/code&gt; will be your &lt;code&gt;GlobalProvider&lt;/code&gt; component. Just as the store is only accessible to the people present in its locality, the &lt;code&gt;GlobalProvider&lt;/code&gt; component wraps the components we want to make the state data accessible to, essentially extending its &quot;locality&quot; to these specific components. ️&lt;/p&gt;
&lt;p&gt;This is just the gist of it all, and I know it seems a little confusing, but when we dive deep into the examples, things will get easier—trust me on this! &lt;/p&gt;
&lt;h3 id=&quot;creating-context&quot;&gt;Creating context&lt;/h3&gt;
&lt;p&gt;Now, the first step is to import the createContext function into our GlobalProvider component. This component will be our store , which will provide its items (state data) to its public (components). Let's take a look at it:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=11548114591033130575&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;In this code, we are creating a context using createContext and passing initialState, which consists of a string 'default value', to it. However, this value gets overridden by another value passed to the value prop of the &lt;code&gt;&amp;lt;GlobalState.Provider&amp;gt;&lt;/code&gt; component. This is actually our Provider component created from the context named &lt;code&gt;GlobalState&lt;/code&gt;. &lt;/p&gt;
&lt;p&gt;In simple terms, to provide our state data to any component, we need to wrap them with the Provider component created from the context name, like &lt;code&gt;GlobalState.Provider&lt;/code&gt; .&lt;/p&gt;
&lt;p&gt;In case you are wondering what the children prop of the &lt;code&gt;GlobalProvider&lt;/code&gt; component means here, it is actually a prop that is available to all components. It refers to any child component of the component using the &lt;code&gt;children&lt;/code&gt; prop.  So, it means that any component wrapped by the &lt;code&gt;GlobalProvider&lt;/code&gt; component will get access to the state variables. This is because we are wrapping the children of the &lt;code&gt;GlobalProvider&lt;/code&gt; component with the provider (GlobalState.Provider) of the state variables. &lt;br&gt;
If you wish to pass more than one value to all the components, you can do so by writing the values as an object in the &lt;code&gt;value&lt;/code&gt; prop like this: &lt;code&gt;value={{ firstValue: 'first value', secondValue: 'second value' }}&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;wrapping-components-by-wrapper-component&quot;&gt;Wrapping components by wrapper component&lt;/h3&gt;
&lt;p&gt;Before we move on to using the values in the components, we need to wrap the components with our wrapper component, which in this case is the GlobalProvider component.  Don't forget the concept of how in order for a person to get access to the items of a store, they need to be present in the store's locality.  This is how you would do that:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=10459000512157752527&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;Also, keep in mind that whenever any of the values passed to the value prop changes, all the child components of the Provider component, the ones using the &lt;code&gt;useContext&lt;/code&gt; hook to extract the values, will re-render  whether they use the values in the jsx or not.&lt;/p&gt;
&lt;h3 id=&quot;using-usecontext-hook-to-access-state-data&quot;&gt;Using useContext hook to access state data&lt;/h3&gt;
&lt;p&gt;Finally, we can now use the values of our state using the &lt;code&gt;useContext&lt;/code&gt; hook of React. This hook expects a context, which in our case is &lt;code&gt;GlobalState&lt;/code&gt; (don't forget to export it from our &lt;code&gt;GlobalState.js&lt;/code&gt; file).&lt;/p&gt;
&lt;p&gt;First, let's take a look at the code :&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=12649371957215901018&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;Here, we simply import the &lt;code&gt;useContext&lt;/code&gt; hook from React and pass &lt;code&gt;GlobalState&lt;/code&gt; (created using &lt;code&gt;createContext(initialState)&lt;/code&gt; imported from &lt;code&gt;GlobalState.js&lt;/code&gt;). Since in the last example we passed an object to the value prop of &lt;code&gt;GlobalState.Provider&lt;/code&gt;, we can now de-structure these values (&lt;code&gt;firstValue&lt;/code&gt; and &lt;code&gt;secondValue&lt;/code&gt;) from the object returned by &lt;code&gt;useContext&lt;/code&gt; hook. In simple terms, the &lt;code&gt;useContext&lt;/code&gt; hook returns the value defined in the value prop of the nearest Provider.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;And there you have it!  I hope this blog has made the tedious process of passing state and props among components a breeze. By harnessing React's useContext and GlobalProvider, managing state becomes seamless and efficient. Now, with cleaner code and reduced complexity, you're ready to build more scalable and organized React applications. If you wish to put this knowledge to a test, then I would suggest making a simple expense tracker  in which you can do the following,&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Make a minimalist UI using multiple components like for one for showing the history of transactions, one for showing the total income , etc.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can create one state variable in your globalProvider component which will then be shared among all the components so that you can use these values to update the balance as well as for recording the history of transactions and much more!&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt; Happy coding! ✨&lt;/p&gt;
</description>
<category>JavaScript</category>
<guid isPermaLink="true">https://coderlegion.com/386/from-props-to-context-simplifying-state-management-in-react</guid>
<pubDate>Sun, 07 Jul 2024 06:47:54 +0000</pubDate>
</item>
</channel>
</rss>