<?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: sajjanbalar</title>
    <description>The latest articles on DEV Community by sajjanbalar (@sajjanbalar).</description>
    <link>https://dev.to/sajjanbalar</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%2F153394%2F1a5d21dc-98a8-49c4-8e6b-eefa4469162b.jpeg</url>
      <title>DEV Community: sajjanbalar</title>
      <link>https://dev.to/sajjanbalar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sajjanbalar"/>
    <language>en</language>
    <item>
      <title>My problem with React Hooks!</title>
      <dc:creator>sajjanbalar</dc:creator>
      <pubDate>Fri, 19 Apr 2019 08:20:00 +0000</pubDate>
      <link>https://dev.to/sajjanbalar/my-problem-with-react-hooks-4kao</link>
      <guid>https://dev.to/sajjanbalar/my-problem-with-react-hooks-4kao</guid>
      <description>&lt;p&gt;Hi,&lt;/p&gt;

&lt;p&gt;Recently, I was writing a new electron app with react. I decided to write it using Hooks. From my experience, I found that hooks are great for the most part. There are however some issues with using it in certain cases. In such cases, I find using classes more useful.&lt;/p&gt;

&lt;p&gt;The one pattern that I found very painful to code in Hooks is when I want to write a custom hook that is passed a callback which it needs to call on certain conditions. I have found that if this callback uses a state variable or more. For every update of the state of this variable, I will have to pass my new callback to the hook. &lt;/p&gt;

&lt;p&gt;This causes two issues. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;I have to remember every prop and state change that will be used by a function and write a useEffect hook to change the callback.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The callback might be used in a complicated way inside the hook and it might be a pain to update it again and again.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I don't know if that makes sense, so let me give you an example.&lt;/p&gt;

&lt;p&gt;I have written a hook that is responsible for handling an eventsource and invoking a callback whenever it receives a message. Let's call this useEventSourceManager.&lt;br&gt;
I like the idea of useEventSourceManager because it gives me the ability to refactor all eventSource management into one hook. &lt;br&gt;
Now, useEventSoucreManager is passed a callback "handleMessage" which is used to render ui for the new messages received.&lt;/p&gt;

&lt;p&gt;My problem with hooks is basically that the following code won't work:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const someFunction = props =&amp;gt; {
  const [condition, setCondition] = useState(false);
  useEventSoureManager(...someProps, ...someStateVariable, handleMessage);

  some other code that will change state of condition variable

  function handleMessage(){
    if(condition){ do something...}
    else{ do something else...}
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The above code doesn't work because the handleMessage instance passed to useEventSourceManager always has the state variable "condition" set to false.&lt;/p&gt;

&lt;p&gt;Also, I get why this happens. And the way to get around is to refresh the callback function. What I trying to point out is that that same code is much easier to write in classes.&lt;/p&gt;

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