<?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: Iman YeckehZaare</title>
    <description>The latest articles on DEV Community by Iman YeckehZaare (@1man).</description>
    <link>https://dev.to/1man</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F858368%2F2a39f8d3-6e2e-4101-9602-241b3388906f.png</url>
      <title>DEV Community: Iman YeckehZaare</title>
      <link>https://dev.to/1man</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/1man"/>
    <language>en</language>
    <item>
      <title>Can a React usestate update be nested in another useState functional update?</title>
      <dc:creator>Iman YeckehZaare</dc:creator>
      <pubDate>Fri, 06 May 2022 00:12:30 +0000</pubDate>
      <link>https://dev.to/1man/can-a-react-usestate-update-be-nested-in-another-usestate-functional-update-3jjf</link>
      <guid>https://dev.to/1man/can-a-react-usestate-update-be-nested-in-another-usestate-functional-update-3jjf</guid>
      <description>&lt;p&gt;There are &lt;code&gt;n&lt;/code&gt; items the user can vote on. There are two React states:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;votes is an array of the user's votes on every item.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;const [votes, setVotes] = useState({});&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;voteChanges indicates how many times the user changed their votes on any of the items.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;const [voteChanges, setVoteChanges] = useState(0);&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;We have a database listener defined in a &lt;code&gt;useEffect&lt;/code&gt;. When votes are added/changed in the database, the listener gets called. We need the state &lt;code&gt;votes&lt;/code&gt; to compare every new vote with its previous value in votes to decide whether to set &lt;code&gt;voteChanges&lt;/code&gt; to &lt;code&gt;true&lt;/code&gt;. However, we cannot define &lt;code&gt;votes&lt;/code&gt; as a dependency in &lt;code&gt;useEffect&lt;/code&gt;, otherwise we will end up in an infinite loop. To get access to the current value of &lt;code&gt;votes&lt;/code&gt;, we use its functional update. Is it okay to call &lt;code&gt;setVoteChanges&lt;/code&gt; inside the functional update of &lt;code&gt;setVotes&lt;/code&gt;? If no, why and how should we solve this problem?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(() =&amp;gt; {
    databaseListener((changes) =&amp;gt; {
      setVotes((oldVotes) =&amp;gt; {
        const oVotes = { ...oldVotes };
        for (let change of changes) {
          const itemId = change.id;
          const newVote = change.vote;
          if (oVotes[itemId] !== newVote) {
            setVoteChanges(oldCount =&amp;gt; oldCount + 1);
          }
          oVotes[itemId] = newVote;
        }
        return oVotes;
      });
    });
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>react</category>
      <category>usestate</category>
      <category>useeffect</category>
      <category>purefunction</category>
    </item>
  </channel>
</rss>
