<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sulav Gaire</title>
    <description>The latest articles on DEV Community by Sulav Gaire (@sulavgaire).</description>
    <link>https://dev.to/sulavgaire</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1458055%2F28c972de-d6e0-460d-a3d3-6302eae8990e.jpeg</url>
      <title>DEV Community: Sulav Gaire</title>
      <link>https://dev.to/sulavgaire</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sulavgaire"/>
    <language>en</language>
    <item>
      <title>React Context API: A Hilarious Journey into State Management 🚀</title>
      <dc:creator>Sulav Gaire</dc:creator>
      <pubDate>Tue, 30 Apr 2024 07:15:42 +0000</pubDate>
      <link>https://dev.to/sulavgaire/react-context-api-a-hilarious-journey-into-state-management-l6p</link>
      <guid>https://dev.to/sulavgaire/react-context-api-a-hilarious-journey-into-state-management-l6p</guid>
      <description>&lt;p&gt;Welcome, Fellow developers, to the wild and wacky world of &lt;strong&gt;React Context API&lt;/strong&gt;, where state management becomes an adventure filled with laughter, tears, and a few &lt;code&gt;console.log()&lt;/code&gt; statements thrown in for good measure. Today, we embark on a journey through the depths of &lt;strong&gt;React's state management&lt;/strong&gt;. Strap in, because this is going to be one heck of a ride!🎉&lt;/p&gt;




&lt;h2&gt;
  
  
  Chapter 1 - In the Beginning, There Was State
&lt;/h2&gt;

&lt;p&gt;Once upon a time, in the land of React, developers found themselves faced with a common dilemma: how to manage state efficiently across multiple components without descending into madness of prop-drilling &lt;a href="https://www.geeksforgeeks.org/what-is-prop-drilling-and-how-to-avoid-it/"&gt;[1]&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Prop-Drilling&lt;/em&gt;: The nightmare scenario where you pass props down through multiple layers of components, akin to navigating a maze blindfolded.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F04rgv2e25ksi28q53ta4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F04rgv2e25ksi28q53ta4.png" alt="prop-drilling" width="612" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Enter the hero of our story: the Context API. With its magical powers, it promised to solve all our state management woes and bring peace to the kingdom of React. 💡&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6l3pxlfzj76yao5f6qc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6l3pxlfzj76yao5f6qc.png" alt="using Context API" width="638" height="456"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Chapter 2: Unveiling the Context API
&lt;/h2&gt;

&lt;p&gt;Discovering the Context API is like finding a treasure map in a sea of code. It offers global state management and component bliss. 🌟&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Three steps to use the context:&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creating the context&lt;/li&gt;
&lt;li&gt;Providing the context&lt;/li&gt;
&lt;li&gt;Consuming the context&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://dmitripavlutin.com/90649ae4bdf379c482ad24e0dd220bc4/react-context-3.svg"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgnwsidum9h6hrze9okg0.png" alt="Context API" width="622" height="515"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A. Creating the context&lt;/strong&gt;&lt;br&gt;
The built-in factory function &lt;code&gt;createContext(default)&lt;/code&gt; creates a context instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// context.js
import { createContext } from 'react';

export const Context = createContext('Default Value');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The factory function accepts one optional argument: the default value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;B. Providing the context&lt;/strong&gt;&lt;br&gt;
Context.Provider component available on the context instance is used to provide the context to its child components, no matter how deep they are. &lt;em&gt;(No more prop drilling)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To set the value of context use the value prop available on the &lt;code&gt;&amp;lt;Context.Provider value={value} /&amp;gt;&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Context } from './context';

function Main() {
  const value = 'My Context Value';
  return (
    &amp;lt;Context.Provider value={value}&amp;gt;
      &amp;lt;MyComponent /&amp;gt;
    &amp;lt;/Context.Provider&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, what's important here is that all the components that'd like later to consume the context have to be wrapped inside the provider component.&lt;/p&gt;

&lt;p&gt;If you want to change the context value, simply update the value prop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C. Consuming the context&lt;/strong&gt;&lt;br&gt;
Consuming the context can be performed in 2 ways.&lt;/p&gt;

&lt;p&gt;The first way, the one I recommend, is to use the &lt;code&gt;useContext(Context)&lt;/code&gt; React hook:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useContext } from 'react';
import { Context } from './context';

function MyComponent() {
  const value = useContext(Context);

  return &amp;lt;span&amp;gt;{value}&amp;lt;/span&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The hook returns the value of the context: &lt;code&gt;value = useContext(Context)&lt;/code&gt;. The hook also makes sure to re-render the component when the context value changes.&lt;/p&gt;

&lt;p&gt;The second way is by using a render function supplied as a child to &lt;code&gt;Context.Consumer&lt;/code&gt; special component available on the context instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Context } from './context';

function MyComponent() {
  return (
    &amp;lt;Context.Consumer&amp;gt;
      {value =&amp;gt; &amp;lt;span&amp;gt;{value}&amp;lt;/span&amp;gt;}
    &amp;lt;/Context.Consumer&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, in case the context value changes, &lt;code&gt;&amp;lt;Context.Consumer&amp;gt;&lt;/code&gt; will re-call its render function.&lt;/p&gt;

&lt;p&gt;You can have as many consumers as you want for a single context. If the context value changes (by changing the value prop of the provider &lt;code&gt;&amp;lt;Context.Provider value={value} /&amp;gt;&lt;/code&gt;), then all consumers are immediately notified and re-rendered.&lt;/p&gt;

&lt;p&gt;If the consumer isn't wrapped inside the provider, but still tries to access the context value (using &lt;code&gt;useContext(Context)&lt;/code&gt; or &lt;code&gt;&amp;lt;Context.Consumer&amp;gt;&lt;/code&gt;), then the value of the context would be the default value argument supplied to &lt;code&gt;createContext(defaultValue)&lt;/code&gt; factory function that had created the context.&lt;br&gt;
With the Consumer component, accessing context values becomes a breeze. Say goodbye to prop drilling and hello to context goodness. 🦸‍♂️&lt;/p&gt;

&lt;h2&gt;
  
  
  Chapter 3: The Future of Context
&lt;/h2&gt;

&lt;p&gt;As our adventure ends, we ponder the Context API's future. Will it remain king, or will new challengers arise? One thing's certain: React devs will always hold it dear. ❤️&lt;/p&gt;

&lt;p&gt;Some common use cases are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;global state&lt;/li&gt;
&lt;li&gt;theme&lt;/li&gt;
&lt;li&gt;application configuration&lt;/li&gt;
&lt;li&gt;authenticated user name&lt;/li&gt;
&lt;li&gt;user settings&lt;/li&gt;
&lt;li&gt;preferred language&lt;/li&gt;
&lt;li&gt;a collection of services&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;note: Zustand, Redux are also used for state management&lt;/em&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;Our journey through React's Context API ends with laughter, tears, and a laptop or two tossed in frustration. But with each challenge conquered, we find joy in the endless possibilities of web development. Happy coding, fellow adventurers! 🚀🎉&lt;/p&gt;




&lt;h2&gt;
  
  
  Refrences
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.geeksforgeeks.org/what-is-prop-drilling-and-how-to-avoid-it/"&gt;[1]&lt;/a&gt; - &lt;a href="https://www.geeksforgeeks.org/what-is-prop-drilling-and-how-to-avoid-it/"&gt;https://www.geeksforgeeks.org/what-is-prop-drilling-and-how-to-avoid-it/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dmitripavlutin.com/react-context-and-usecontext/"&gt;[2]&lt;/a&gt; - &lt;a href="https://dmitripavlutin.com/react-context-and-usecontext/"&gt;https://dmitripavlutin.com/react-context-and-usecontext/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.freecodecamp.org/news/context-api-in-react/"&gt;[3]&lt;/a&gt; - &lt;a href="https://www.freecodecamp.org/news/context-api-in-react/"&gt;https://www.freecodecamp.org/news/context-api-in-react/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://react.dev/reference/react/createContext"&gt;[4]&lt;/a&gt; - &lt;a href="https://react.dev/reference/react/createContext"&gt;https://react.dev/reference/react/createContext&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>contextapi</category>
      <category>react</category>
      <category>statemanagement</category>
    </item>
  </channel>
</rss>
