<?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: Christina Gaitán</title>
    <description>The latest articles on DEV Community by Christina Gaitán (@christinagaitan).</description>
    <link>https://dev.to/christinagaitan</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%2F6408%2Fd431199b-b166-4638-8fec-3d310fdf75bf.jpg</url>
      <title>DEV Community: Christina Gaitán</title>
      <link>https://dev.to/christinagaitan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/christinagaitan"/>
    <language>en</language>
    <item>
      <title>React - Updating self-dependent state</title>
      <dc:creator>Christina Gaitán</dc:creator>
      <pubDate>Wed, 26 Jun 2019 18:13:06 +0000</pubDate>
      <link>https://dev.to/christinagaitan/react-updating-self-dependent-state-44nk</link>
      <guid>https://dev.to/christinagaitan/react-updating-self-dependent-state-44nk</guid>
      <description>&lt;p&gt;In React, sometimes we need to update a state value taking into account the current state.&lt;/p&gt;

&lt;p&gt;For example, if we are writing a Poll app and we have a button or another component to update the number of votes, we can do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;incrementVotes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;currentVotes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;votes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;votes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;currentVotes&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The above code could work, but considering that &lt;code&gt;setState()&lt;/code&gt; is an asynchronous function, a better solution is to send a function to &lt;code&gt;setState()&lt;/code&gt; as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nx"&gt;incrementVotes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevState&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;votes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;prevState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;votes&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;With the first approach, we have the risk that our &lt;code&gt;state&lt;/code&gt; does not update immediately, for example if our votes count is 10 and we have a user that is too fast clicking the button, we could have two calls to &lt;code&gt;incrementVotes()&lt;/code&gt; in which the value of &lt;code&gt;this.state.votes&lt;/code&gt; is the same and in the end we have a final value of 11 when the correct value should be 12.&lt;/p&gt;

&lt;p&gt;Maybe there is a thin probability of this occurring but this is a good way of protecting our application of this kind of bugs.&lt;/p&gt;

</description>
      <category>react</category>
    </item>
  </channel>
</rss>
