<?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: Jayant Khandelwal</title>
    <description>The latest articles on DEV Community by Jayant Khandelwal (@jackent2b).</description>
    <link>https://dev.to/jackent2b</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%2F543369%2Fbcf435a7-38cd-407b-b521-2c27cd71c530.jpeg</url>
      <title>DEV Community: Jayant Khandelwal</title>
      <link>https://dev.to/jackent2b</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jackent2b"/>
    <language>en</language>
    <item>
      <title>Re-rendering in react</title>
      <dc:creator>Jayant Khandelwal</dc:creator>
      <pubDate>Mon, 24 Jul 2023 17:05:26 +0000</pubDate>
      <link>https://dev.to/jackent2b/re-rendering-in-react-4gha</link>
      <guid>https://dev.to/jackent2b/re-rendering-in-react-4gha</guid>
      <description>&lt;p&gt;A child component is re-rendered in react in three cases:&lt;br&gt;
1.) If the parent component is re-rendered, then the child will be re-rendered.&lt;br&gt;
2.) If the state of the parent component is updated using useState or useReducer. -&amp;gt; then parent component will re-render, then the child will be re-rendered.&lt;br&gt;
3.) If the props of child component is updated, it will be re-rendered. (It is the main point, all other point are related to this point only.)&lt;/p&gt;

&lt;p&gt;we can make use of React.memo or passing child as a prop to prevent form unnecessary re-rendering.&lt;br&gt;
(passing child as a prop will also work beacuse apart from React.memo because a component can not change its own prop)&lt;/p&gt;

&lt;p&gt;Also, when we pass objects or function as a prop which remains same, then to avoid unnecessary rendering of child component, we can make use of useCallback and useMemo hook.&lt;/p&gt;

&lt;p&gt;This is because when we pass objects or function as prop from parent component, when the parent component re-renders due to some state change, a new refernce to object and function is created, which will be treated as a new prop that to be passed to the child component. So to prevent that, we make use of useMemo(for non primitive values like object and arrat) and useCallback(for functions) &lt;/p&gt;

</description>
    </item>
    <item>
      <title>What the hell is CDN?</title>
      <dc:creator>Jayant Khandelwal</dc:creator>
      <pubDate>Sun, 10 Jul 2022 09:12:21 +0000</pubDate>
      <link>https://dev.to/jackent2b/what-the-hell-is-cdn-14ff</link>
      <guid>https://dev.to/jackent2b/what-the-hell-is-cdn-14ff</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhbk0f05q68kt79xw8yzc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhbk0f05q68kt79xw8yzc.png" alt="Content delivery Network Image" width="520" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Recently, the Cloudflare outage knocked many popular services such as Coinbase, Discord, Udemy etc. But do you know what Cloudflare is? 🤔&lt;/p&gt;

&lt;p&gt;Cloudflare is a CDN company.✔️&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ok But, What do you mean by CDNs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A content delivery network (CDN) refers to a geographically distributed group of servers that work together to deliver Internet content quickly.&lt;/p&gt;

&lt;p&gt;CDN allows the quick transfer of assets needed for loading Internet content including HTML pages, javascript files, stylesheets, images, and videos.&lt;/p&gt;

&lt;p&gt;Major giants like Meta, Netflix, and Amazon etc serve the majority of their traffic through CDNs.&lt;/p&gt;

&lt;p&gt;🤔&lt;strong&gt;Ok, so what are the benefits of CDN?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Improving website load times. - By distributing content closer to website visitors by using a nearby CDN server, visitors experience faster page loading times.&lt;/p&gt;

&lt;p&gt;Reducing bandwidth costs- Bandwidth consumption costs for website hosting are a primary expense for websites.  CDNs are able to reduce the amount of data an origin server must provide through caching and other optimizations, thus reducing hosting costs for website owners.&lt;/p&gt;

&lt;p&gt;Increasing content availability and redundancy - Large amounts of traffic or hardware failures can interrupt normal website function. A CDN can handle more traffic and withstand hardware failure better than many origin servers due to its distributed nature.&lt;/p&gt;

&lt;p&gt;🤔&lt;strong&gt;Now tell me, how do CDN works?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;CDN is a network of servers linked together with the goal of delivering content as quickly, cheaply, reliably, and securely as possible. &lt;/p&gt;

&lt;p&gt;In order to improve speed and connectivity, a CDN will place servers at the exchange points between different networks.&lt;/p&gt;

&lt;p&gt;Internet exchange points (IXPs) are the primary locations where different Internet providers connect in order to provide each other access to traffic originating on their different networks.&lt;/p&gt;

&lt;p&gt;By having a connection to these high-speed and highly interconnected locations, a CDN provider is able to reduce costs and transit times in high-speed data delivery.😉&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CDNs are Best!&lt;/strong&gt;😉&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>cdn</category>
      <category>network</category>
      <category>webdev</category>
    </item>
    <item>
      <title>The Box Model</title>
      <dc:creator>Jayant Khandelwal</dc:creator>
      <pubDate>Fri, 04 Feb 2022 13:20:20 +0000</pubDate>
      <link>https://dev.to/jackent2b/the-box-model-3a19</link>
      <guid>https://dev.to/jackent2b/the-box-model-3a19</guid>
      <description>&lt;p&gt;Whether you're a rookie, an experienced developer or someone in the center of the stack, one of the most crucial CSS concepts to comprehend is "𝐓𝐡𝐞 𝐁𝐨𝐱 𝐌𝐨𝐝𝐞𝐥."&lt;/p&gt;

&lt;p&gt;Q.) What Is &lt;strong&gt;"The Box Model"&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;➡️ "According to the box model concept, every element on a page is a rectangular box and may have width, height, padding, borders, and margins."&lt;/p&gt;

&lt;p&gt;𝐊𝐞𝐞𝐩 𝐢𝐧 𝐌𝐢𝐧𝐝: Every element on a page is a rectangular box.&lt;/p&gt;

&lt;p&gt;Every element is a rectangular box, and there are several properties that determine the size of that box. &lt;/p&gt;

&lt;p&gt;The core of the box is defined by the width and height of an element, which may be determined by 🧵:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The 𝙙𝙞𝙨𝙥𝙡𝙖𝙮 property, or 
2.The 𝙘𝙤𝙣𝙩𝙚𝙣𝙩 of the element, or
3.The specified 𝙬𝙞𝙙𝙩𝙝 and 𝙝𝙚𝙞𝙜𝙝𝙩 properties.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;𝙥𝙖𝙙𝙙𝙞𝙣𝙜 and  𝙗𝙤𝙧𝙙𝙚𝙧, expand the dimensions of the box outward from the element’s width and height. &lt;/p&gt;

&lt;p&gt;𝙢𝙖𝙧𝙜𝙞𝙣 we have specified will follow the border.&lt;/p&gt;

&lt;p&gt;Each part of the box model corresponds to a CSS property:  𝙬𝙞𝙙𝙩𝙝, 𝙝𝙚𝙞𝙜𝙝𝙩, 𝙥𝙖𝙙𝙙𝙞𝙣𝙜, 𝙗𝙤𝙧𝙙𝙚𝙧, and 𝙢𝙖𝙧𝙜𝙞𝙣!&lt;/p&gt;

&lt;p&gt;The size of the box itself is calculated like this:&lt;/p&gt;

&lt;p&gt;𝗪𝗶𝗱𝘁𝗵 = width + padding-left + padding-right + border-left + border-right + margin-left + margin-right&lt;/p&gt;

&lt;p&gt;𝗛𝗲𝗶𝗴𝗵𝘁 = height + padding-top + padding-bottom + border-top + border-bottom + margin-top + margin-bottom&lt;/p&gt;

&lt;p&gt;Happy Coding!!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgv70cu3wgzofkzzvf9o4.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgv70cu3wgzofkzzvf9o4.jpeg" alt="Box Model Image" width="800" height="611"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>javascript</category>
      <category>html</category>
      <category>boxmo</category>
    </item>
    <item>
      <title>Performance optimization Hook: *useCallback* and *useMemo* hook!</title>
      <dc:creator>Jayant Khandelwal</dc:creator>
      <pubDate>Thu, 24 Dec 2020 12:13:24 +0000</pubDate>
      <link>https://dev.to/jackent2b/usecallback-hooks-egl</link>
      <guid>https://dev.to/jackent2b/usecallback-hooks-egl</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is&lt;/strong&gt; &lt;em&gt;useCallback&lt;/em&gt; &lt;strong&gt;hook?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;useCallback&lt;/em&gt; hook is a hook that will return a memorized version of the callback function that only changes if one of the dependencies has changed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why do we use&lt;/strong&gt; &lt;em&gt;useCallback&lt;/em&gt; &lt;strong&gt;hook?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It is useful when passing callbacks to optimized child components that rely on reference equality to prevent unnecessary renders!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is&lt;/strong&gt; &lt;em&gt;useMemo&lt;/em&gt; &lt;strong&gt;hook?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;useMemo&lt;/em&gt; hook is very similar to &lt;em&gt;useCallback&lt;/em&gt; hook but the difference is that &lt;em&gt;useMemo&lt;/em&gt; hook invokes the provided function and caches its results.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;useCallback vs useMemo&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you need to cache a function, then use&lt;/em&gt; &lt;strong&gt;useCallback&lt;/strong&gt; &lt;em&gt;hook whereas,&lt;/em&gt;&lt;br&gt;
&lt;em&gt;If you need to cache the result of the invoked function use&lt;/em&gt; &lt;strong&gt;useMemo&lt;/strong&gt; &lt;em&gt;hook.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Happy Coding!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;-Jayant Khandelwal&lt;/em&gt;  &lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>hooks</category>
      <category>usecallback</category>
    </item>
    <item>
      <title>The best Couple: useContext + useReducer !</title>
      <dc:creator>Jayant Khandelwal</dc:creator>
      <pubDate>Wed, 23 Dec 2020 18:14:42 +0000</pubDate>
      <link>https://dev.to/jackent2b/the-best-couple-usecontext-usereducer-4e65</link>
      <guid>https://dev.to/jackent2b/the-best-couple-usecontext-usereducer-4e65</guid>
      <description>&lt;p&gt;&lt;em&gt;Disclaimer:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This is going to be one of the best post you have ever come across in search of &lt;em&gt;useContext + useReducer&lt;/em&gt; hooks!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why do we use useContext and usereducer together?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Reason:&lt;/em&gt; To share states between the components i.e. Global State &lt;br&gt;
 Management&lt;/p&gt;

&lt;p&gt;In the previous posts, we have seen how to make use of useContext and useReducer hooks.&lt;/p&gt;

&lt;p&gt;So, in this post, we will see how to make use of them &lt;em&gt;together!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkwmuw3qj5jz3wcwix1u4.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkwmuw3qj5jz3wcwix1u4.JPG" alt="Alt Text" width="800" height="398"&gt;&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;In this example, we are going to share the state of the counter between Component A , Component D and Component F!&lt;/p&gt;

&lt;p&gt;Also, the Components hierarchy in our example is as follows: &lt;/p&gt;

&lt;p&gt;1.)Component A&lt;br&gt;
2.)Component B -&amp;gt; Component D&lt;br&gt;
3.)Component C -&amp;gt; Component E -&amp;gt; Component F&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Steps:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1.) Create &lt;em&gt;React context&lt;/em&gt; in the main &lt;em&gt;&lt;/em&gt; component&lt;/p&gt;

&lt;p&gt;2.) Make reducer function in the main &lt;em&gt;&lt;/em&gt; component&lt;/p&gt;

&lt;p&gt;3.) Make use of the useReducer function.&lt;/p&gt;

&lt;p&gt;4.) Send context value using &lt;em&gt;Context Provider&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;5.) Consume Context Value using &lt;em&gt;useContext&lt;/em&gt; hook.&lt;/p&gt;

&lt;p&gt;6.) Transfer action from the components using &lt;em&gt;dispatch&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Okay so let's get started!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step1:&lt;/strong&gt; Create &lt;em&gt;React Context&lt;/em&gt; and &lt;em&gt;reducer()&lt;/em&gt; function:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ftn1hhnrzxrxn7yth8o8f.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ftn1hhnrzxrxn7yth8o8f.JPG" alt="Alt Text" width="800" height="562"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step2:&lt;/strong&gt; In the functional component, call &lt;em&gt;useReducer(reducer,initialState)&lt;/em&gt; hook :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fe7o0vyc66tmskxahlhia.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fe7o0vyc66tmskxahlhia.JPG" alt="Alt Text" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, you can see that we have passed the state i.e. &lt;em&gt;counter&lt;/em&gt; and &lt;em&gt;dispatch&lt;/em&gt; method as the value in the &lt;em&gt;Context Provider.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Now, in order to access the global state in Component A, Component D, and Component F, we need to make use of the useContext Hook to consume the context value passed by the &lt;em&gt;Context Provider&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(component A)&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhcqdw4lt6s2q07xn3no5.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhcqdw4lt6s2q07xn3no5.JPG" alt="Alt Text" width="800" height="515"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(component D)&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fccjg9mco1cw4wtxqkmkx.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fccjg9mco1cw4wtxqkmkx.JPG" alt="Alt Text" width="800" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(component F)&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fz355nnwd7rl9bypg2qzg.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fz355nnwd7rl9bypg2qzg.JPG" alt="Alt Text" width="800" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; In order to change the global state through Components A, D, and F, we have passed &lt;em&gt;action&lt;/em&gt; (i.e. increment, decrement, or reset) through the &lt;em&gt;dispatch&lt;/em&gt; function.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This is all you need to manage the global state using&lt;/em&gt; &lt;strong&gt;useReducer and useContext&lt;/strong&gt; &lt;em&gt;hook!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I hope it might have helped you!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Do share your thaughts!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;*Happy Coding, Thank you! *&lt;/p&gt;

&lt;p&gt;&lt;em&gt;-Jayant Khandelwal&lt;/em&gt;&lt;/p&gt;

</description>
      <category>context</category>
      <category>reducer</category>
      <category>react</category>
      <category>hooks</category>
    </item>
    <item>
      <title>Fetch using useReducer!</title>
      <dc:creator>Jayant Khandelwal</dc:creator>
      <pubDate>Wed, 23 Dec 2020 12:18:20 +0000</pubDate>
      <link>https://dev.to/jackent2b/fetch-using-usereducer-7h0</link>
      <guid>https://dev.to/jackent2b/fetch-using-usereducer-7h0</guid>
      <description>&lt;p&gt;In this post, we will gonna see how to make a &lt;em&gt;Fetch&lt;/em&gt; request with the help of the &lt;em&gt;useReducer&lt;/em&gt; hook.&lt;/p&gt;

&lt;p&gt;This is our reducer function,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fusd2u2tfa7pt46bavuak.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fusd2u2tfa7pt46bavuak.JPG" alt="Alt Text" width="800" height="680"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's have a look at our functional component,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fv7hz85g28ok3ctmhbqc0.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fv7hz85g28ok3ctmhbqc0.JPG" alt="Alt Text" width="800" height="493"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Complete Code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpgexetdtom2zlicw7omw.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpgexetdtom2zlicw7omw.JPG" alt="Alt Text" width="800" height="753"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I hope it might help you!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Happy coding!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;-Jayant khandelwal&lt;/em&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>hooks</category>
      <category>usereducer</category>
    </item>
    <item>
      <title>useReducer Vs useState (The War)</title>
      <dc:creator>Jayant Khandelwal</dc:creator>
      <pubDate>Tue, 22 Dec 2020 18:46:57 +0000</pubDate>
      <link>https://dev.to/jackent2b/usereducer-vs-usestate-the-war-b2d</link>
      <guid>https://dev.to/jackent2b/usereducer-vs-usestate-the-war-b2d</guid>
      <description>&lt;p&gt;In this post, we are going to see the difference between &lt;em&gt;useReducer&lt;/em&gt; and &lt;em&gt;useState&lt;/em&gt; hooks and when to use them.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fk0z9r0fyhojaytokogf2.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fk0z9r0fyhojaytokogf2.JPG" alt="Alt Text" width="800" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Hope it might help you guys&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Happy Coding!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thank You&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;-Jayant Khandelwal&lt;/em&gt;&lt;/p&gt;

</description>
      <category>hooks</category>
      <category>react</category>
      <category>webdev</category>
      <category>reducer</category>
    </item>
    <item>
      <title>useReducer final destination (Signing Off):</title>
      <dc:creator>Jayant Khandelwal</dc:creator>
      <pubDate>Tue, 22 Dec 2020 18:39:39 +0000</pubDate>
      <link>https://dev.to/jackent2b/usereducer-final-destination-signing-off-56kc</link>
      <guid>https://dev.to/jackent2b/usereducer-final-destination-signing-off-56kc</guid>
      <description>&lt;p&gt;&lt;strong&gt;Welcome Back&lt;/strong&gt; devs!&lt;/p&gt;

&lt;p&gt;We have talked about useReducer hook, its usage, and its implementation in the previous post of this series.&lt;/p&gt;

&lt;p&gt;First of all, let us have a &lt;em&gt;recap&lt;/em&gt;,&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Roles&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;action&lt;/strong&gt;: Action is a plain JavaScript string or object that contains information. Actions tell the reducer what kind of action to perform and all other fields contain information or data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;initialState&lt;/strong&gt;: defines the initial state of the component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;currentState&lt;/strong&gt;: defines the current state of the component&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;reducer&lt;/strong&gt;: reducer() function accepts the currentState and action and then returns a new state based on that action&lt;/p&gt;

&lt;p&gt;&lt;em&gt;useReducer&lt;/em&gt; hook returns the currentState and the dispatch method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;dispatch&lt;/strong&gt;: dispatch method is capable of accepting an action to execute the code specified in the render function i.e. it accepts the action and transfers it to the reducer function.&lt;/p&gt;

&lt;p&gt;Note: initialization of initialState and reducer() is always made outside the functional component.&lt;/p&gt;

&lt;p&gt;Now, in this post, we are going to see another method that can be used to use the &lt;strong&gt;useReducer&lt;/strong&gt; hook.&lt;/p&gt;

&lt;p&gt;Let's have look at our reducer,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7efmwin3gk67r8yjvki3.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7efmwin3gk67r8yjvki3.JPG" alt="reducer" width="751" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Reducer&lt;/em&gt; function is the same as we have implemented in the first post of usReducer hook&lt;/p&gt;

&lt;p&gt;Now, let's see our functional component,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsrevtt0bhabkzwx43fvf.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsrevtt0bhabkzwx43fvf.JPG" alt="Functional component" width="800" height="466"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, we have implemented &lt;em&gt;two&lt;/em&gt; different &lt;em&gt;useReducer&lt;/em&gt; hook using the same &lt;em&gt;reducer()&lt;/em&gt; function. &lt;br&gt;
This is another way to make use of the &lt;em&gt;useReducer&lt;/em&gt; hook.&lt;/p&gt;

&lt;p&gt;Let's have a look at the complete code,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsvv0ibh0b2aezit2n6st.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsvv0ibh0b2aezit2n6st.JPG" alt="cmpltcode" width="800" height="745"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0knsghfdsdt1ckwub7s0.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0knsghfdsdt1ckwub7s0.JPG" alt="Alt Text" width="362" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So with this, we come to the end of the &lt;em&gt;useReducer&lt;/em&gt; hook.&lt;/p&gt;

&lt;p&gt;We have seen &lt;em&gt;three&lt;/em&gt; different methods to use and implement the &lt;em&gt;useReducer&lt;/em&gt; hook.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Feel free to leave a comment if you face any doubt!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Happy Coding!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thank you,&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;- Jayant Khandelwal&lt;/em&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>hooks</category>
      <category>reducer</category>
    </item>
    <item>
      <title>useReducer (The Intermediate):</title>
      <dc:creator>Jayant Khandelwal</dc:creator>
      <pubDate>Tue, 22 Dec 2020 17:51:52 +0000</pubDate>
      <link>https://dev.to/jackent2b/usereducer-part-2-continue-272g</link>
      <guid>https://dev.to/jackent2b/usereducer-part-2-continue-272g</guid>
      <description>&lt;p&gt;&lt;strong&gt;Welcome back&lt;/strong&gt; devs!&lt;/p&gt;

&lt;p&gt;We have talked about useReducer hook, its usage, and its implementation in the previous post of this series.&lt;/p&gt;

&lt;p&gt;Now in this post, we will make use of useReducer in a slightly complex manner.&lt;/p&gt;

&lt;p&gt;First of all let us have a &lt;em&gt;recap&lt;/em&gt;,&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Roles&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;action&lt;/strong&gt;: Action is a plain JavaScript string or object that contains information. Actions tell the reducer what kind of action to perform and all other fields contain information or data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;initialState&lt;/strong&gt;: defines the initial state of the component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;currentState&lt;/strong&gt;: defines the current state of the component&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;reducer&lt;/strong&gt;: reducer() function accepts the currentState and action and then returns a new state based on that action&lt;/p&gt;

&lt;p&gt;&lt;em&gt;useReducer&lt;/em&gt; hook returns the currentState and the dispatch method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;dispatch&lt;/strong&gt;: dispatch method is capable of accepting an action to execute the code specified in the render function i.e. it accepts the action and transfers it to the reducer function.&lt;/p&gt;

&lt;p&gt;Note: initialization of initialState and reducer() is always made outside the functional component.&lt;/p&gt;

&lt;p&gt;First of all, let's initialize our initialState and implement our reducer function as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F24yeybe07buykxnhyry2.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F24yeybe07buykxnhyry2.JPG" alt="Alt Text" width="800" height="487"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see that this time we have two states to maintain that is &lt;em&gt;firstCounter&lt;/em&gt; and &lt;em&gt;secondCounter&lt;/em&gt;. So, in order to maintain two states, we have used an object to initialize the state. &lt;/p&gt;

&lt;p&gt;We have used &lt;strong&gt;spread operator&lt;/strong&gt; because useReducer &lt;strong&gt;does not&lt;/strong&gt; automatically merge and update the object i.e. useReducer does not merge the state automatically.&lt;/p&gt;

&lt;p&gt;Now let's take a look at the functional component,&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fd296hmws4907x2bldxeb.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fd296hmws4907x2bldxeb.JPG" alt="Functional Component" width="800" height="668"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we have passed &lt;em&gt;action&lt;/em&gt; as an object. We generally pass action as an object when we have more than one property to be accepted by the reducer function.&lt;/p&gt;

&lt;p&gt;As here we are maintaining two states, therefore we are sending two different type property (eg: &lt;em&gt;increment&lt;/em&gt; and &lt;em&gt;increment2&lt;/em&gt;) for &lt;br&gt;
firstCounter and secondCounter respectively.&lt;/p&gt;

&lt;p&gt;Let's have a complete look at the code,&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fia257a83t5v8k6f3g7v0.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fia257a83t5v8k6f3g7v0.JPG" alt="Alt Text" width="800" height="768"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flc14213fx7edjkks4obr.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flc14213fx7edjkks4obr.JPG" alt="Alt Text" width="527" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;We will see one more way to use the **useReducer&lt;/em&gt;* hook in the upcoming posts of this series.*&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Stay Tuned!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Happy Coding!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thank you!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;-Jayant Khandelwal&lt;/em&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>hooks</category>
      <category>context</category>
    </item>
    <item>
      <title>Getting started with useReducer Hook!</title>
      <dc:creator>Jayant Khandelwal</dc:creator>
      <pubDate>Tue, 22 Dec 2020 13:26:35 +0000</pubDate>
      <link>https://dev.to/jackent2b/getting-started-with-usereducer-hook-4e0b</link>
      <guid>https://dev.to/jackent2b/getting-started-with-usereducer-hook-4e0b</guid>
      <description>&lt;p&gt;&lt;em&gt;useReducer Hook is also used for state management!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;-&amp;gt; It is an alternative to &lt;em&gt;useState&lt;/em&gt;  (useState is built using useReducer)&lt;/p&gt;

&lt;p&gt;-&amp;gt; What's the difference between the two?&lt;/p&gt;

&lt;p&gt;-&amp;gt; When to useReducer v useState?&lt;/p&gt;

&lt;p&gt;Have you ever came across the &lt;em&gt;reduce()&lt;/em&gt; function in JavaScrpt?&lt;/p&gt;

&lt;p&gt;Let us make a contrast between &lt;em&gt;reduce&lt;/em&gt; and &lt;em&gt;useReducer&lt;/em&gt; hook.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdzo5qn40lmi4sfbi59xw.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdzo5qn40lmi4sfbi59xw.JPG" alt="Alt Text" width="800" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;-&amp;gt; &lt;strong&gt;reducer&lt;/strong&gt;(currentState,action)&lt;br&gt;
-&amp;gt; &lt;strong&gt;useReducer&lt;/strong&gt;(reducer,initialState)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Roles&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;action&lt;/strong&gt;: Action is a plain JavaScript string or object that contains information. Actions tell the reducer what kind of action to perform and all other fields contain information or data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;initialState&lt;/strong&gt;: defines the initial state of the component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;currentState&lt;/strong&gt;: defines the current state of the component&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;reducer&lt;/strong&gt;: reducer() function accepts the currentState and action and then returns a new state based on that action&lt;/p&gt;

&lt;p&gt;&lt;em&gt;useReducer&lt;/em&gt; hook returns the currentState and the dispatch method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;dispatch&lt;/strong&gt;: dispatch method is capable of accepting an action to execute the code specified in the render function i.e. it accepts the action and transfers it to the reducer function.&lt;/p&gt;

&lt;p&gt;Now, let us have a look at our code!&lt;/p&gt;

&lt;p&gt;First of all, let's make the &lt;em&gt;reducer()&lt;/em&gt; function and initialize an &lt;em&gt;initialState&lt;/em&gt;.&lt;br&gt;
&lt;strong&gt;Note&lt;/strong&gt;: initialization of &lt;em&gt;initialState&lt;/em&gt; and &lt;em&gt;reducer()&lt;/em&gt; is always made outside the functional component.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdueqdh4c0fk8no2oxqvz.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdueqdh4c0fk8no2oxqvz.JPG" alt="reducer fnction" width="679" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's make use of the &lt;em&gt;useReducer&lt;/em&gt; hook.&lt;/p&gt;

&lt;p&gt;Call a &lt;em&gt;useReducer&lt;/em&gt; hook inside the functional component and pass &lt;em&gt;reducer&lt;/em&gt; and &lt;em&gt;initialState&lt;/em&gt; as arguments in it. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhzkuygy066wkc7n5wn5m.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhzkuygy066wkc7n5wn5m.JPG" alt="Alt Text" width="800" height="324"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;let's have a look at the complete code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Furv0wbs6k06ziksqqfyx.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Furv0wbs6k06ziksqqfyx.JPG" alt="complete" width="800" height="681"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Following is the output for the above code,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffmcvwnvp5ocv1dlo89tp.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffmcvwnvp5ocv1dlo89tp.JPG" alt="output" width="334" height="81"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;We will see two more ways to use&lt;/em&gt; &lt;strong&gt;useReducer&lt;/strong&gt; &lt;em&gt;hook in the upcoming posts&lt;/em&gt;.&lt;br&gt;
&lt;em&gt;Stay Tuned!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Happy Coding!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thank you!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;-Jayant Khandelwal&lt;/em&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>hook</category>
      <category>context</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Making use of useContext Hook!</title>
      <dc:creator>Jayant Khandelwal</dc:creator>
      <pubDate>Tue, 22 Dec 2020 11:24:08 +0000</pubDate>
      <link>https://dev.to/jackent2b/making-use-of-usecontext-hook-46bh</link>
      <guid>https://dev.to/jackent2b/making-use-of-usecontext-hook-46bh</guid>
      <description>&lt;p&gt;&lt;em&gt;Welcome back devs!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We have talked about using ContextAPI, how to create context, how to Provide context value, and how to Consume context value in the last post of the series.&lt;/p&gt;

&lt;p&gt;In this post, we are going to see how to make use of &lt;em&gt;useContext&lt;/em&gt; Hook.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Description:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;useContext accepts a context object (the value returned from React.createContext) and returns the current context value for that context. The current context value is determined by the value prop of the nearest  above the calling component in the tree.&lt;/p&gt;

&lt;p&gt;When the nearest  above the component updates, this Hook will trigger a rerender with the latest context value passed to that MyContext provider&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Following are the changes you need to make to our previous code in our earlier code in order to use&lt;/em&gt; &lt;strong&gt;useContext&lt;/strong&gt; &lt;em&gt;hook:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fikpztwz5idw194htvryg.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fikpztwz5idw194htvryg.JPG" alt="useContext" width="800" height="322"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;vs&lt;/p&gt;

&lt;p&gt;Before,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fuxr566pklh4u6u6e1iba.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fuxr566pklh4u6u6e1iba.JPG" alt="without useContext" width="800" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Only this code you have to change, the rest all remains the same as it is.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note&lt;/em&gt;: useContext(MyContext) only lets you read the context and subscribe to its changes. You still need a  above in the tree to provide the value for this context.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Stay tuned for the upcoming posts of the Series!!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I hope it might help you, Feel free to leave a comment if you have any doubt!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Feedbacks are always welcome!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Happy Coding!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;-Jayant Khandelwal&lt;/em&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>hook</category>
      <category>context</category>
    </item>
    <item>
      <title>Context ApI + useContext Hook (The deadly Beast)!!</title>
      <dc:creator>Jayant Khandelwal</dc:creator>
      <pubDate>Tue, 22 Dec 2020 10:59:04 +0000</pubDate>
      <link>https://dev.to/jackent2b/context-api-usecontext-hook-easiest-way-possible-cem</link>
      <guid>https://dev.to/jackent2b/context-api-usecontext-hook-easiest-way-possible-cem</guid>
      <description>&lt;p&gt;&lt;strong&gt;Fun Fact:&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Context API to State management in React, is Hammer to Hulk!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Don't you agree?&lt;/strong&gt;&lt;br&gt;
Check it out yourself :&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"Context provides a way to pass data through the component tree without having to pass props down manually at every level."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2g8yb9vpwm5y0rqytlkf.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2g8yb9vpwm5y0rqytlkf.JPG" alt="Tree" width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Consider a Scenairo:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the above Tree, if we need to access the &lt;em&gt;username&lt;/em&gt; in &lt;em&gt;Component A, Component D, and Component F&lt;/em&gt; then these can be the following ways:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Method 1.)&lt;/strong&gt;&lt;br&gt;
Pass it down through all the components as props i.e (B -&amp;gt; D) and (C -&amp;gt; E -&amp;gt; F). The Problem with this method is that we need to unnecessarily pass props through &lt;em&gt;Component B, Component C, and Component E&lt;/em&gt; as they are the &lt;em&gt;Parent Component&lt;/em&gt; of Component D and F. It decreases the efficiency of our software.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Method 2.)&lt;/strong&gt;  Use Context API&lt;/p&gt;

&lt;p&gt;We will make use of &lt;em&gt;Method 2&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Steps to Create Context&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1.) Create the context&lt;br&gt;
2.) Provide the context value&lt;br&gt;
3.) Consume the context value&lt;/p&gt;

&lt;p&gt;Let us consider only the case from &lt;em&gt;(C -&amp;gt; E -&amp;gt; F)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:)&lt;/strong&gt; Create the Context:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fu2ilp5foddgvocv90knc.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fu2ilp5foddgvocv90knc.JPG" alt="Create Context" width="800" height="288"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the first Step, we have made a context using &lt;em&gt;React.createContext()&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2.)&lt;/strong&gt; Provide the context value:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwj62grerdoppzj29mgqu.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwj62grerdoppzj29mgqu.JPG" alt="Provider" width="800" height="411"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, we have used the &lt;em&gt;Context Provider&lt;/em&gt; to pass the &lt;em&gt;value&lt;/em&gt; to the components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;em&gt;value&lt;/em&gt; passed in &lt;em&gt;Provider&lt;/em&gt; will be passed to all the components (and their Child Components) present inside the &lt;em&gt;Provider Tag&lt;/em&gt;. In our case, the value will be passed to Component C and its child Components (i.e &lt;em&gt;Component E&lt;/em&gt; and &lt;em&gt;Component F&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3.)&lt;/strong&gt; Consume the context value:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ftjhq8xak1jfuvosjzs1c.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ftjhq8xak1jfuvosjzs1c.JPG" alt="Consume Context" width="800" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we need to &lt;em&gt;Consume&lt;/em&gt; the &lt;em&gt;context value&lt;/em&gt; which we have passed down to Component C and its child Components. We will consume it in Component F.&lt;/p&gt;

&lt;p&gt;In the return statement, place a &lt;em&gt;Consumer&lt;/em&gt; tag and enclose the JSX function in it having the context value as arguments (&lt;em&gt;username&lt;/em&gt; as an argument in our case).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7fxlkhxyyzc95q96k7l3.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7fxlkhxyyzc95q96k7l3.JPG" alt="Alt Text" width="454" height="216"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is how you can make use of Context API.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;It can be noted that there is a little bit of hassle in order to consume context value&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In order to make consumption of context value an easy task, we will make use of the&lt;/em&gt; &lt;strong&gt;useContext&lt;/strong&gt; &lt;em&gt;hook in the upcoming post!&lt;/em&gt;  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stay tuned for the upcoming posts of the Series!!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I hope it might help you, Feel free to leave a comment if you have any doubt!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Feedbacks are always welcome!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Happy Coding!&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;&lt;em&gt;-Jayant Khandelwal&lt;/em&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>hooks</category>
      <category>context</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
