<?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: Sebin CM</title>
    <description>The latest articles on DEV Community by Sebin CM (@sebzz).</description>
    <link>https://dev.to/sebzz</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%2F785796%2Fba86de2e-03cd-4586-a750-81e320113c06.jpeg</url>
      <title>DEV Community: Sebin CM</title>
      <link>https://dev.to/sebzz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sebzz"/>
    <language>en</language>
    <item>
      <title>Closures and useEffects</title>
      <dc:creator>Sebin CM</dc:creator>
      <pubDate>Wed, 22 Jun 2022 12:49:57 +0000</pubDate>
      <link>https://dev.to/sebzz/closures-and-useeffects-1ai7</link>
      <guid>https://dev.to/sebzz/closures-and-useeffects-1ai7</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState, useEffect} from "react";

export default function App() {
  const [count, setCount] = useState(0);

   useEffect(() =&amp;gt; {
     setTimeout(() =&amp;gt; {
       console.log(" ", count);
    }, 3000);
   }, []);

  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;p&amp;gt;{count}&amp;lt;/p&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; setCount(count + 1)}&amp;gt;Click Me &amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code will display the value of count when the component renders. In the meantime if we click the button and try to change the value of count. The value of count changes but the log will contain the value zero. This also happens when you are working with async function.&lt;/p&gt;

&lt;p&gt;We can get around this problem by using useRef hook. The code is as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState, useEffect, useRef } from "react";

export default function App() {
  const [count, setCount] = useState(0);
  const countRef = useRef(0);

  countRef.current = count;

  useEffect(() =&amp;gt; {
    setTimeout(() =&amp;gt; {
      console.log("useRef", countRef.current);
    }, 3000);
  }, []);

  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;p&amp;gt;{count}&amp;lt;/p&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; setCount(count + 1)}&amp;gt;Click Me &amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;using the above block of code we can get the current value of the variable while logging it to the console&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>useref</category>
      <category>useeffect</category>
    </item>
    <item>
      <title>Looking for an open-source mentor</title>
      <dc:creator>Sebin CM</dc:creator>
      <pubDate>Sun, 16 Jan 2022 18:38:50 +0000</pubDate>
      <link>https://dev.to/sebzz/looking-for-an-open-source-mentor-3b0a</link>
      <guid>https://dev.to/sebzz/looking-for-an-open-source-mentor-3b0a</guid>
      <description>&lt;p&gt;I am very much interested in contributing to open source and I am looking for a mentor who can guide me through the basics and help me understand the codebse. I cant wait for my first PR. I would love to work with java java-script or golang.&lt;br&gt;
Thank-you&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
