<?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: Prashant Jha</title>
    <description>The latest articles on DEV Community by Prashant Jha (@pbucky).</description>
    <link>https://dev.to/pbucky</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%2F724783%2Fb591776f-7633-4f08-962c-cee0f505356f.jpg</url>
      <title>DEV Community: Prashant Jha</title>
      <link>https://dev.to/pbucky</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pbucky"/>
    <language>en</language>
    <item>
      <title>Let's make a primitive useState Hook</title>
      <dc:creator>Prashant Jha</dc:creator>
      <pubDate>Sat, 19 Mar 2022 19:18:40 +0000</pubDate>
      <link>https://dev.to/pbucky/lets-make-a-primitive-usestate-hook-aj2</link>
      <guid>https://dev.to/pbucky/lets-make-a-primitive-usestate-hook-aj2</guid>
      <description>&lt;h2&gt;
  
  
  &lt;code&gt;useState&lt;/code&gt; A magical hook or an 'Array'?, lets find out.
&lt;/h2&gt;

&lt;p&gt;Let's make a function which will return a useState function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const React = () =&amp;gt; {
  return {
    useState: () =&amp;gt; {}, 
  };
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Declare a &lt;code&gt;let&lt;/code&gt; variable inside React function, this variable will store our state value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const React = () =&amp;gt; {
  let stateValue;
  return {
    useState: () =&amp;gt; {}, 
  };
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's do some quick coding.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const React = () =&amp;gt; {
  let stateValue;
  return {
   useState: (initialState) =&amp;gt; {
     stateValue = stateValue || initialState;
     const setState = (newStateValue) =&amp;gt; {
       stateValue = newStateValue;
     };
     return [stateValue, setState];
    },
  };
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In above code, we are returning an array from &lt;code&gt;useState&lt;/code&gt; in which &lt;code&gt;stateValue&lt;/code&gt; is a variable which will hold our current state value, and &lt;code&gt;setState&lt;/code&gt; is a function this will accept a updated value and set it to &lt;code&gt;stateValue&lt;/code&gt;, this is how the state going to update.&lt;/p&gt;

&lt;p&gt;This was the all concept, you can checkout the working code &lt;a href="https://codesandbox.io/s/cold-wind-jr2q73?file=/src/index.js" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Comment below for any sort of question from that code.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Stale Closures in React.useEffect() Hook "A Weird Bug for New React Learners"</title>
      <dc:creator>Prashant Jha</dc:creator>
      <pubDate>Mon, 07 Mar 2022 19:58:26 +0000</pubDate>
      <link>https://dev.to/pbucky/stale-closures-in-reactuseeffect-hook-a-weird-bug-for-new-react-learners-2n7k</link>
      <guid>https://dev.to/pbucky/stale-closures-in-reactuseeffect-hook-a-weird-bug-for-new-react-learners-2n7k</guid>
      <description>&lt;p&gt;&lt;em&gt;It was a beautiful day, my day was going almost good but suddenly...&lt;br&gt;
I encountered stale closures in React.useEffect()&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This was my code, ahh... a simple and small code but good enough to frustrate you. &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%2F86b301jlbi0vtnd62ha2.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%2F86b301jlbi0vtnd62ha2.PNG" alt="Stale Closure Bug" width="559" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you run this code, It will show &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hello Bucky&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;on screen.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Haha, Nah It'll not&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The thing is it'll only show Hello + "", I mean only Hello.&lt;br&gt;
Why?&lt;br&gt;
because of closures. &lt;br&gt;
Hey, what is closures?&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=qikxEIxsXco" rel="noopener noreferrer"&gt;Checkout this video&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Okay got it? That video on closure was amazing isn't it...&lt;br&gt;
Now let's continue.&lt;/p&gt;

&lt;p&gt;Here are two ways you can solve this problem&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The useEffect's dependency array&lt;br&gt;
&amp;amp;&lt;br&gt;
Using a ref instead of a useState&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;The useEffect's dependency array&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%2Fuploads%2Farticles%2Fke95e9h8qny6fx5ou6xv.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%2Fke95e9h8qny6fx5ou6xv.PNG" alt="Closure issue solve using useEffect's dependency array" width="557" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On line 15 just put user in dependency array, so from now whenever user value will get updated the useEffect() will render again and new value of user will get set in line 14.&lt;/p&gt;

&lt;p&gt;But, this approach will set greeting twice one with previous("") user value and one with updated("Bucky") user value as useEffect() will render twice.&lt;/p&gt;

&lt;p&gt;Let's look into another better solution to tackle this stale closures issue.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using a ref instead of a useState&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%2Fuploads%2Farticles%2F775btkljk2lqv54pa3f2.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%2F775btkljk2lqv54pa3f2.PNG" alt="Closure issue solve using a ref instead of a useState" width="571" height="456"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What is useRef()?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=t2ypzz6gJm0" rel="noopener noreferrer"&gt;Watch this video&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As, in previous approach useEffect() was rendering twice, but in this useEffect() will only run once.&lt;/p&gt;

&lt;p&gt;If you are thinking which is better, I believe it's depend more upon on your use case.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Cool... Now It's end, if you're having any suggestion in this article please update me in comment, I'll love to update this article with more better information.&lt;/em&gt; Thankyou Have a great day.&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%2Fxj8mz72kwtlctqb0nes2.gif" 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%2Fxj8mz72kwtlctqb0nes2.gif" alt="Thankyou, bye" width="90" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
