<?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: Sushil Kumar</title>
    <description>The latest articles on DEV Community by Sushil Kumar (@sushil-kumar).</description>
    <link>https://dev.to/sushil-kumar</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%2F3302792%2Faa0c1da9-4470-437c-8a31-96842db0ff7a.jpg</url>
      <title>DEV Community: Sushil Kumar</title>
      <link>https://dev.to/sushil-kumar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sushil-kumar"/>
    <language>en</language>
    <item>
      <title>🔍 Deep Dive: `Object.hasOwn()` Your Safer `hasOwnProperty` Alternative</title>
      <dc:creator>Sushil Kumar</dc:creator>
      <pubDate>Wed, 23 Jul 2025 18:11:34 +0000</pubDate>
      <link>https://dev.to/sushil-kumar/deep-dive-objecthasown-your-safer-hasownproperty-alternative-3mdk</link>
      <guid>https://dev.to/sushil-kumar/deep-dive-objecthasown-your-safer-hasownproperty-alternative-3mdk</guid>
      <description>&lt;p&gt;In JavaScript, checking whether an object has a property of its own (and not inherited from its prototype chain) is a common task. &lt;/p&gt;

&lt;p&gt;Traditionally, developers have used &lt;code&gt;Object.prototype.hasOwnProperty.call(obj, key)&lt;/code&gt; or the shorthand &lt;code&gt;obj.hasOwnProperty(key)&lt;/code&gt;. With ES2022, a new, more ergonomic method arrived: &lt;code&gt;Object.hasOwn&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In this article, we’ll explore:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why &lt;code&gt;Object.hasOwn&lt;/code&gt; exists
&lt;/li&gt;
&lt;li&gt;How it compares to legacy approaches
&lt;/li&gt;
&lt;li&gt;Syntax and usage examples
&lt;/li&gt;
&lt;li&gt;Polyfill for older environments
&lt;/li&gt;
&lt;li&gt;Performance considerations
&lt;/li&gt;
&lt;li&gt;Browser &amp;amp; environment support
&lt;/li&gt;
&lt;li&gt;Best practices
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  📜 Background: The Legacy Approach
&lt;/h2&gt;

&lt;p&gt;Before ES2022, to check if an object &lt;code&gt;obj&lt;/code&gt; has its own property &lt;code&gt;prop&lt;/code&gt;, you typically did:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hasOwnProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prop&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// …&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, this can break if &lt;code&gt;obj&lt;/code&gt; shadows or deletes the inherited method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;hasOwnProperty&lt;/span&gt;&lt;span class="p"&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="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hasOwnProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// false — wrong!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To guard against that, the safest pattern was:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hasOwnProperty&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// obj truly has "foo" as its own property&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But the syntax is verbose and repetitive. Enter &lt;code&gt;Object.hasOwn&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  🚀 Introducing &lt;code&gt;Object.hasOwn&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The ES2022 standard introduced a static method on &lt;code&gt;Object&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hasOwn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;prop&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;obj&lt;/code&gt;&lt;/strong&gt;: The target object
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;prop&lt;/code&gt;&lt;/strong&gt;: The property key (string or symbol)
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It returns &lt;code&gt;true&lt;/code&gt; if &lt;code&gt;obj&lt;/code&gt; has its own (non-inherited) property named &lt;code&gt;prop&lt;/code&gt;, and &lt;code&gt;false&lt;/code&gt; otherwise.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hasOwn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hasOwn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;toString&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// false (inherited)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🔄 Polyfill for Older Environments
&lt;/h2&gt;

&lt;p&gt;If you need support in environments without &lt;code&gt;Object.hasOwn&lt;/code&gt;, you can add a small polyfill:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hasOwn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hasOwn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;prop&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hasOwnProperty&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;prop&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;Include this once at the top of your codebase (or in a setup file) to safely use &lt;code&gt;Object.hasOwn&lt;/code&gt; everywhere.&lt;/p&gt;




&lt;h2&gt;
  
  
  ⚖️ Performance Considerations
&lt;/h2&gt;

&lt;p&gt;Under the hood, &lt;code&gt;Object.hasOwn(obj, prop)&lt;/code&gt; calls the same algorithm as &lt;code&gt;hasOwnProperty&lt;/code&gt;. Benchmarks indicate negligible difference in performance. The main advantage is &lt;strong&gt;readability&lt;/strong&gt; and &lt;strong&gt;safety&lt;/strong&gt; against shadowed properties.&lt;/p&gt;




&lt;h2&gt;
  
  
  🌐 Browser &amp;amp; Environment Support
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Environment&lt;/th&gt;
&lt;th&gt;Support&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Chrome&lt;/td&gt;
&lt;td&gt;93+&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Firefox&lt;/td&gt;
&lt;td&gt;92+&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Safari&lt;/td&gt;
&lt;td&gt;15.4+&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Node.js&lt;/td&gt;
&lt;td&gt;16.9+&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For older runtimes, include the polyfill above.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧰 Best Practices
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prefer &lt;code&gt;Object.hasOwn&lt;/code&gt;&lt;/strong&gt; for clarity and safety.
&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;descriptive variable names&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hasOwn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cartItem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;quantity&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// handle quantity&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Avoid &lt;code&gt;in&lt;/code&gt; operator&lt;/strong&gt; when you need to exclude inherited props:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;toString&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;            &lt;span class="c1"&gt;// true (inherited)  &lt;/span&gt;
  &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hasOwn&lt;/span&gt;&lt;span class="p"&gt;({},&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;toString&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🏁 Conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Object.hasOwn&lt;/code&gt; brings a concise, reliable way to check for own properties in objects—guarding against prototype pitfalls and improving code readability. By adopting this modern method (and polyfilling where necessary), you can write cleaner, more maintainable JavaScript.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔗 Reference
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwn" rel="noopener noreferrer"&gt;MDN: Object.hasOwn&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;Happy coding! 👨‍💻👩‍💻&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>ecmascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Demystifying React’s setState Batching: Why Your Counter Only Jumps Once</title>
      <dc:creator>Sushil Kumar</dc:creator>
      <pubDate>Sat, 28 Jun 2025 12:02:49 +0000</pubDate>
      <link>https://dev.to/sushil-kumar/demystifying-reacts-setstate-batching-why-your-counter-only-jumps-once-9c3</link>
      <guid>https://dev.to/sushil-kumar/demystifying-reacts-setstate-batching-why-your-counter-only-jumps-once-9c3</guid>
      <description>&lt;p&gt;Ever wondered why this sequence only bumps your counter by &lt;strong&gt;1&lt;/strong&gt;, not 2?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&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="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// still the old value!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It can feel like React’s hiding some magic, so let’s unpack what’s really happening.&lt;/p&gt;




&lt;h2&gt;
  
  
  1️⃣ Batched &amp;amp; Deferred Updates
&lt;/h2&gt;

&lt;p&gt;React &lt;em&gt;queues&lt;/em&gt; all state updates that happen inside the same event handler, then applies them in one render pass. That means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You enqueue your changes immediately, but&lt;/li&gt;
&lt;li&gt;The actual UI update happens &lt;strong&gt;after&lt;/strong&gt; your function finishes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So you don’t see the new state right away, and that’s why calling &lt;code&gt;setCount&lt;/code&gt; twice doesn’t give you two separate renders.&lt;/p&gt;




&lt;h2&gt;
  
  
  2️⃣ Immediate Enqueue (No Hidden Timers)
&lt;/h2&gt;

&lt;p&gt;Each call to &lt;code&gt;setCount(...)&lt;/code&gt; runs &lt;strong&gt;synchronously&lt;/strong&gt;. There’s no &lt;code&gt;setTimeout&lt;/code&gt; under the hood. React simply adds your update to an internal queue. As soon as you invoke &lt;code&gt;setCount&lt;/code&gt;, React remembers “we need to update state” and waits for the right moment to flush.&lt;/p&gt;




&lt;h2&gt;
  
  
  3️⃣ Same Snapshot, Same Result
&lt;/h2&gt;

&lt;p&gt;Inside one render cycle, &lt;code&gt;count&lt;/code&gt; stays at its initial value (say, &lt;code&gt;0&lt;/code&gt;). Both of your updates see that same &lt;code&gt;0&lt;/code&gt; and each calculate “0 + 1.” When React finally processes the queue, it applies two identical updates, ending up at &lt;strong&gt;1&lt;/strong&gt;, not 2.&lt;/p&gt;




&lt;h2&gt;
  
  
  4️⃣ The Reliable Fix: Functional Updates
&lt;/h2&gt;

&lt;p&gt;Whenever you need to base a new state on the previous value, use the functional form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;c&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="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;c&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, each updater receives the &lt;em&gt;latest&lt;/em&gt; pending state (&lt;code&gt;c&lt;/code&gt;), so you reliably end up with &lt;strong&gt;+2&lt;/strong&gt;.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;💡 &lt;strong&gt;What to remember:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“Async” feel = React batches + defers renders&lt;/li&gt;
&lt;li&gt;“Sync” reality = setter calls enqueue instantly&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pro tip:&lt;/strong&gt; Always prefer functional updaters when relying on the previous state&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;




&lt;p&gt;🔗 &lt;strong&gt;Reference:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://react.dev/learn/queueing-a-series-of-state-updates" rel="noopener noreferrer"&gt;Queueing a Series of State Updates&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have you run into this gotcha? Share your experience or other React surprises below! 👇&lt;/p&gt;

&lt;p&gt;Happy coding! 🚀&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>javascript</category>
      <category>react</category>
      <category>usestate</category>
    </item>
  </channel>
</rss>
