<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Muhammad Fareed Javed on Medium]]></title>
        <description><![CDATA[Stories by Muhammad Fareed Javed on Medium]]></description>
        <link>https://medium.com/@fareedjaved203?source=rss-f85e2d6a75bd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Vlbsllydm45-__KsWiGMaA.png</url>
            <title>Stories by Muhammad Fareed Javed on Medium</title>
            <link>https://medium.com/@fareedjaved203?source=rss-f85e2d6a75bd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 10:35:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@fareedjaved203/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Race Condition in React useState]]></title>
            <link>https://medium.com/@fareedjaved203/race-condition-in-react-usestate-777e72c64cb7?source=rss-f85e2d6a75bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/777e72c64cb7</guid>
            <category><![CDATA[race-condition]]></category>
            <category><![CDATA[callback]]></category>
            <category><![CDATA[usestate]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[asynchronous-functions]]></category>
            <dc:creator><![CDATA[Muhammad Fareed Javed]]></dc:creator>
            <pubDate>Fri, 28 Jul 2023 12:39:04 GMT</pubDate>
            <atom:updated>2023-07-28T12:39:04.381Z</atom:updated>
            <content:encoded><![CDATA[<p>Race condition is a problem that arises when multiple parts of a program attempt to access the same shared code concurrently. The final result of the shared code depends on the order in which these multiple parts of the program access it simultaneously.</p><p>Consider an example of race condition problem in React useState.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*Kgtt-p72eryt3OyCfY8YFw.png" /><figcaption>Race Condition problem in React useState</figcaption></figure><p>In the above piece of code, there are 2 buttons. One that instantly updates the state variable and the other that has an asynchronous Timeout function with a delay of 3 seconds.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*bpU9AZi2yTYIsG1LeCpNoQ.gif" /><figcaption>UI for Race Condition problem</figcaption></figure><p>In the above gif, ‘Delayed Increment’ button is clicked first and then ‘Instant Increment’ button is clicked 3 times. After 3 seconds, we get a value of 1 instead of 4.</p><h4>Solution</h4><p>The above problem can be solved using the functional update form in the useState function (setCount).</p><p>The code will be updated as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/532/1*MzcEQEnywCGK6gmwOj7Kmw.png" /><figcaption>Race Condition Solution for useState</figcaption></figure><p>A function is passed that takes the previous value of ‘count’ variable as an argument and returns a new value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*zC-brTje9cApkcdcwsxLig.gif" /><figcaption>UI for Race Condition Solution</figcaption></figure><h4>Note</h4><p>setCount(count + 1) in the setTimeout function, the value of count is determined at the time of the <em>creation of the callback</em>. <br>However, setCount((prevCount)=&gt; prevCount + 1) determines the value of the count at the<em> execution of the callback</em>. That is why we are able to witness the latest value.</p><p>email: fareedjaved203@gmail.com</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=777e72c64cb7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>