<?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"><channel><title><![CDATA[Afaqscripts]]></title><description><![CDATA[Afaqscripts]]></description><link>https://afaqscripts.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 06 Sep 2026 10:37:51 GMT</lastBuildDate><atom:link href="https://afaqscripts.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Unlocking React's Hidden Gems: A Beginner's Guide to useContext and createContext]]></title><description><![CDATA[Are you a budding React developer looking to take your skills to the next level? Do you find yourself struggling with prop drilling and complex state management in your React applications? If so, you've come to the right place.
Before diving into the...]]></description><link>https://afaqscripts.hashnode.dev/unlocking-reacts-hidden-gems-a-beginners-guide-to-usecontext-and-createcontext</link><guid isPermaLink="true">https://afaqscripts.hashnode.dev/unlocking-reacts-hidden-gems-a-beginners-guide-to-usecontext-and-createcontext</guid><category><![CDATA[ReactHooks]]></category><category><![CDATA[Frontend Development]]></category><category><![CDATA[React developer]]></category><category><![CDATA[react js]]></category><category><![CDATA[useContext]]></category><dc:creator><![CDATA[Afaq Ali]]></dc:creator><pubDate>Wed, 13 Sep 2023 06:16:36 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/npxXWgQ33ZQ/upload/ccdc6bb715dcbe4c4d7cd39727d02e16.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Are you a budding React developer looking to take your skills to the next level? Do you find yourself struggling with prop drilling and complex state management in your React applications? If so, you've come to the right place.</p>
<p>Before diving into the <code>useContext</code> hook in React, there are two key concepts you should understand, which will make grasping the concept easier.</p>
<p><strong>Concept 1: Object Destructuring</strong></p>
<p>One way to access values within an object is by object destructuring. Suppose you have an object like this:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> person = { <span class="hljs-attr">name</span>: <span class="hljs-string">"afaq"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">19</span> };
</code></pre>
<p>You can extract values like this:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> { name, age } = person;
</code></pre>
<p>Remember, the spelling of keys must match for successful restructuring</p>
<p><strong>Concept 2: Dynamic Children Values in React Components</strong></p>
<p>Imagine you want to create a reusable button component, but you don't want the button text to be the same every time. To achieve this, you can create a component for the button:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">MyButton</span>(<span class="hljs-params">{ children }</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span>&gt;</span>{children}<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span>;
}
</code></pre>
<p>By using the <code>children</code> prop, you can customize the button text when using the component.</p>
<p><strong>Understanding the Need for</strong> <code>useContext</code></p>
<p>To understand a new concept, it's crucial to know why you need it. Let's consider an example:</p>
<p>Suppose you have a state called "name" in one component:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> [name, setName] = useState(<span class="hljs-string">"afaq"</span>);
</code></pre>
<p>Now, you want to use this state in another component. If it's just one or two components, you can pass it as a prop. But in larger projects with many components, passing props becomes impractical.</p>
<p><strong>Introducing the useContext Hook</strong></p>
<p>This is where the <code>useContext</code> hook in React comes to the rescue. It uses the concept of passing state like we did with props but extends it to the entire React app, making the state accessible in any component.</p>
<p><strong>Using createContext and useContext</strong></p>
<p>Here's how to use <code>createContext</code> and <code>useContext</code>:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// In your main file (e.g., index.js)</span>
<span class="hljs-keyword">import</span> React, { createContext, useContext } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;

<span class="hljs-keyword">const</span> MyContext = createContext();

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> name = <span class="hljs-string">"afaq"</span>; <span class="hljs-comment">// Replace with your state</span>
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">MyContext.Provider</span> <span class="hljs-attr">value</span>=<span class="hljs-string">{name}</span>&gt;</span>
      {/* Your app's components */}
    <span class="hljs-tag">&lt;/<span class="hljs-name">MyContext.Provider</span>&gt;</span></span>
  );
}

<span class="hljs-comment">// In any component where you need the value</span>
<span class="hljs-keyword">import</span> React, {  useContext } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">MyComponent</span>(<span class="hljs-params"></span>) </span>{

  <span class="hljs-keyword">const</span> name = useContext(MyContext);

  <span class="hljs-keyword">return</span> (
   <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>My name is {name}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span>
}
</code></pre>
<p>By wrapping your app's components with <code>MyContext.Provider</code>, you can access the value anywhere using <code>useContext</code>.</p>
<p><strong>Taking It Further: Organizing Your Context</strong></p>
<p>In real-world projects, you'll have multiple states and functions. To keep things tidy, create a separate file, e.g., <code>Context.jsx</code>, to define your context:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Context.jsx</span>
<span class="hljs-keyword">import</span> React, { createContext, useContext } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> MyContext = createContext();

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">MyContextProvider</span>(<span class="hljs-params">{ children }</span>) </span>{
  <span class="hljs-keyword">const</span> name = <span class="hljs-string">"afaq"</span>; <span class="hljs-comment">// Replace with your </span>
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">MyContext.Provider</span> <span class="hljs-attr">value</span>=<span class="hljs-string">{name}</span>&gt;</span>
      {children}
    <span class="hljs-tag">&lt;/<span class="hljs-name">MyContext.Provider</span>&gt;</span></span>
  );
}

<span class="hljs-comment">// In your main file (e.g., index.js)</span>
<span class="hljs-keyword">import</span> {  MyContext } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Context"</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{

<span class="hljs-keyword">const</span> { name } = useContext(MyContext)
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>My name is {name} <span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  );
}
</code></pre>
<p>Now, you can organize your context and make it easily accessible across your app.</p>
<p><strong>Being practical : Passing</strong> <code>useState</code> in the Context Value</p>
<p>Consider a scenario where you have a state variable <code>count</code></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> [count, setCount] = useState(<span class="hljs-number">0</span>);
</code></pre>
<p>You want to share this <code>count</code> value with other components. To do this, you can pass it in the context value:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// In your context file (e.g., Context.jsx)</span>
<span class="hljs-keyword">import</span> React, { createContext, useContext, useState } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;

<span class="hljs-keyword">const</span> CountContext = createContext();

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">CountProvider</span>(<span class="hljs-params">{ children }</span>) </span>{
  <span class="hljs-keyword">const</span> [count, setCount] = useState(<span class="hljs-number">0</span>);

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">CountContext.Provider</span> <span class="hljs-attr">value</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">count</span>, <span class="hljs-attr">setCount</span> }}&gt;</span> 
      {children}
    <span class="hljs-tag">&lt;/<span class="hljs-name">CountContext.Provider</span>&gt;</span></span>
  );
}
</code></pre>
<p>Here, we wrap our <code>count</code> state and its <code>setCount</code> function in the context provider.</p>
<p><strong>Note:</strong> <em>When passing multiple values make sure to use double curly brackets {{ count, setCount }} like this, as you can see in the above code otherwise you will not be able get these values in your component because Javascript won't know that you are passing set of objects to it.</em></p>
<p><strong>Using</strong> <code>count</code> <strong>in Other Components</strong></p>
<p>Now, in any component where you want to access and modify the <code>count</code> state, you can use <code>useContext</code> to retrieve it:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// In a component where you want to use 'count'</span>
<span class="hljs-keyword">import</span> React, { useContext } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> { CountContext } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Context"</span>; <span class="hljs-comment">// Import your context</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">CountDisplay</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> { count , setCount} = useContext(CountContext); <span class="hljs-comment">// Access 'count' from the context</span>

<span class="hljs-keyword">return</span> (
   <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>Count: {count}<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>;
     <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> setCount(count + 1)}&gt;Increment Count<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
   <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>Here, we import the <code>CountContext</code> and use <code>useContext</code> to access the <code>count</code> state and the <code>setCount</code> function. You can now use them in your components as needed.</p>
<p><strong>Conclusion:</strong></p>
<p>Now you've learned how to share and manage state using <code>useState</code> and <code>useContext</code>. This powerful combination allows you to centralize state management and easily share it across different parts of your React application.</p>
<p>Feel free to ask if you have any questions or need further clarification. Happy coding!</p>
]]></content:encoded></item></channel></rss>