<?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>Forem: mayank sagar</title>
    <description>The latest articles on Forem by mayank sagar (@mayankrsagar).</description>
    <link>https://forem.com/mayankrsagar</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%2F1795171%2F429d670d-1fe6-42dd-914c-0175683de6ec.png</url>
      <title>Forem: mayank sagar</title>
      <link>https://forem.com/mayankrsagar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mayankrsagar"/>
    <language>en</language>
    <item>
      <title>🎯 Choosing Between WebSocket and Socket.io: A Decision Every Dev Faces</title>
      <dc:creator>mayank sagar</dc:creator>
      <pubDate>Sun, 18 Jan 2026 05:35:03 +0000</pubDate>
      <link>https://forem.com/mayankrsagar/choosing-between-websocket-and-socketio-a-decision-every-dev-faces-3g2d</link>
      <guid>https://forem.com/mayankrsagar/choosing-between-websocket-and-socketio-a-decision-every-dev-faces-3g2d</guid>
      <description>&lt;p&gt;As developers, we don’t just build features.&lt;br&gt;
We battle networks, devices, edge cases, and unpredictable real-world behavior.&lt;/p&gt;

&lt;p&gt;Last week, I hit a familiar challenge while building a real-time module:&lt;br&gt;
Should I go raw WebSocket or use Socket.io?&lt;/p&gt;

&lt;p&gt;Here’s the truth many devs learn the hard way.&lt;/p&gt;




&lt;p&gt;⚡ The Developer Pain&lt;/p&gt;

&lt;p&gt;WebSocket is elegant… until the real world walks in.&lt;/p&gt;

&lt;p&gt;• Sudden network drops&lt;br&gt;
• Mobile switching between Wi-Fi and 4G&lt;br&gt;
• Firewalls blocking traffic&lt;br&gt;
• Manual reconnection loops&lt;br&gt;
• Browser quirks&lt;br&gt;
• Silent failures&lt;/p&gt;

&lt;p&gt;You start with clean code and end up managing a rescue mission.&lt;/p&gt;




&lt;p&gt;🛡 The Practical Developer Choice&lt;/p&gt;

&lt;p&gt;This is where Socket.io quietly becomes a lifesaver.&lt;/p&gt;

&lt;p&gt;• Auto-reconnects&lt;br&gt;
• Fallbacks when WebSocket fails&lt;br&gt;
• Built-in events&lt;br&gt;
• Rooms and namespaces&lt;br&gt;
• Handles the chaos you don’t want to touch&lt;/p&gt;

&lt;p&gt;Not perfect. Not the fastest.&lt;br&gt;
But absolutely reliable in real-world products.&lt;/p&gt;




&lt;p&gt;⚖️ When to Choose What&lt;/p&gt;

&lt;p&gt;Go WebSocket when:&lt;br&gt;
✔ You need raw performance&lt;br&gt;
✔ You control the entire environment&lt;br&gt;
✔ Simplicity matters more than resilience&lt;/p&gt;

&lt;p&gt;Go Socket.io when:&lt;br&gt;
✔ Your users are unpredictable&lt;br&gt;
✔ Networks won’t behave&lt;br&gt;
✔ You want dev-time peace instead of firefights&lt;/p&gt;




&lt;p&gt;🧩 The Dev Lesson&lt;/p&gt;

&lt;p&gt;Real-time systems aren’t about “what works in theory”.&lt;br&gt;
They’re about “what survives the real world”.&lt;/p&gt;

&lt;p&gt;If your app touches real users, don’t choose based on purity.&lt;br&gt;
Choose based on resilience.&lt;/p&gt;

&lt;p&gt;What’s your experience with real-time systems?&lt;br&gt;
Drop your battle stories below.&lt;/p&gt;

&lt;h1&gt;
  
  
  WebSocket #SocketIO #SystemDesign #WebDevelopment #RealTimeApps #BackendDevelopment #NodeJS #Engineering
&lt;/h1&gt;

</description>
      <category>webdev</category>
      <category>node</category>
      <category>backenddevelopment</category>
      <category>mern</category>
    </item>
    <item>
      <title>I Built a Search Engine That Understands Meaning, Not Just Keywords</title>
      <dc:creator>mayank sagar</dc:creator>
      <pubDate>Wed, 14 Jan 2026 10:47:18 +0000</pubDate>
      <link>https://forem.com/mayankrsagar/i-built-a-search-engine-that-understands-meaning-not-just-keywords-1jfb</link>
      <guid>https://forem.com/mayankrsagar/i-built-a-search-engine-that-understands-meaning-not-just-keywords-1jfb</guid>
      <description>&lt;p&gt;I was frustrated with traditional search. 😤&lt;/p&gt;

&lt;p&gt;You search "AI" → No results&lt;br&gt;
But your docs are full of "machine learning", "neural networks", "deep learning"&lt;/p&gt;

&lt;p&gt;Sound familiar?&lt;/p&gt;

&lt;p&gt;That's when I discovered Vector Embeddings and built a solution.&lt;/p&gt;

&lt;p&gt;🎯 The Problem:&lt;br&gt;
Keyword search is dumb. It looks for exact matches, not meaning.&lt;br&gt;
Users search one way. Your content uses different words.&lt;br&gt;
Result? Missed opportunities and frustrated users.&lt;/p&gt;

&lt;p&gt;💡 The Solution:&lt;br&gt;
I built a Semantic Search API that understands CONTEXT, not just keywords.&lt;/p&gt;

&lt;p&gt;Here's what I learned:&lt;/p&gt;

&lt;p&gt;1️⃣ Text → Numbers&lt;br&gt;
Converted documents into 768-dimensional vectors using HuggingFace&lt;br&gt;
Similar meanings = Similar numbers&lt;/p&gt;

&lt;p&gt;2️⃣ Smart Matching&lt;br&gt;
MongoDB Atlas compares vectors, not words&lt;br&gt;
Finds semantically similar content automatically&lt;/p&gt;

&lt;p&gt;3️⃣ Ranked Results&lt;br&gt;
Added metadata boosting (category, date, author)&lt;br&gt;
Most relevant results come first&lt;/p&gt;

&lt;p&gt;🔧 Built with:&lt;br&gt;
• Node.js &amp;amp; Express&lt;br&gt;
• MongoDB Atlas Vector Search&lt;br&gt;
• HuggingFace Embeddings&lt;br&gt;
• MVC Architecture&lt;/p&gt;

&lt;p&gt;📈 Real Impact:&lt;br&gt;
✅ Search "programming" → finds "JavaScript", "Python", "coding"&lt;br&gt;
✅ Works across languages and synonyms&lt;br&gt;
✅ Powers modern AI apps (ChatGPT-style search, RAG systems)&lt;/p&gt;

&lt;p&gt;This project changed how I think about search.&lt;br&gt;
It's not about matching text. It's about understanding intent.&lt;/p&gt;

&lt;p&gt;🔗 Open-sourced on GitHub: [link]&lt;br&gt;
Fully documented for anyone learning AI/ML&lt;/p&gt;

&lt;p&gt;Have you faced similar search problems?&lt;br&gt;
What solutions did you try?&lt;/p&gt;

&lt;h1&gt;
  
  
  AI #MachineLearning #SemanticSearch #ProblemSolving #SoftwareEngineering #NodeJS #MongoDB #OpenToWork #TechInnovation
&lt;/h1&gt;

&lt;p&gt;P.S. - Recruiters: I'm passionate about building AI-powered solutions. Let's connect! 🚀&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>node</category>
      <category>semanticsearch</category>
      <category>opentowork</category>
    </item>
    <item>
      <title>⚠️ JavaScript Equality Is Lying to You</title>
      <dc:creator>mayank sagar</dc:creator>
      <pubDate>Fri, 09 Jan 2026 15:42:43 +0000</pubDate>
      <link>https://forem.com/mayankrsagar/javascript-equality-is-lying-to-you-228a</link>
      <guid>https://forem.com/mayankrsagar/javascript-equality-is-lying-to-you-228a</guid>
      <description>&lt;p&gt;⚠️ JavaScript Equality Is Lying to You&lt;/p&gt;

&lt;p&gt;(Not because of ===… but because of ==)&lt;/p&gt;

&lt;p&gt;Most developers know this rule:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Always use === instead of ==”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But few know WHY == behaves the way it does 🤯&lt;br&gt;
Let’s expose the weird parts.&lt;/p&gt;




&lt;p&gt;🔍 Fact #1: == Follows a Hidden Algorithm (Not Logic)&lt;/p&gt;

&lt;p&gt;When you write:&lt;/p&gt;

&lt;p&gt;[] == false   // true 😵&lt;/p&gt;

&lt;p&gt;JavaScript secretly does:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;[] → ""&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"" → 0&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;false → 0&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;0 == 0 // true&lt;/p&gt;

&lt;p&gt;This is Abstract Equality Comparison.&lt;br&gt;
Most devs never read it.&lt;br&gt;
But JS always follows it.&lt;/p&gt;




&lt;p&gt;🔍 Fact #2: null Is Special (Very Special)&lt;/p&gt;

&lt;p&gt;null == undefined   // true&lt;br&gt;
null == 0           // false&lt;br&gt;
null == false       // false&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;null only loosely equals undefined and nothing else.&lt;/p&gt;

&lt;p&gt;This one breaks assumptions in condition checks:&lt;/p&gt;

&lt;p&gt;if (value == null) {&lt;br&gt;
  // catches null AND undefined&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Many libraries rely on this intentionally.&lt;/p&gt;




&lt;p&gt;🔍 Fact #3: NaN Hates Everyone (Even Itself)&lt;/p&gt;

&lt;p&gt;NaN === NaN  // false&lt;br&gt;
NaN == NaN   // false&lt;/p&gt;

&lt;p&gt;Only way to detect it:&lt;/p&gt;

&lt;p&gt;Number.isNaN(NaN) // true&lt;/p&gt;

&lt;p&gt;If you’ve ever compared calculations directly…&lt;br&gt;
you’ve already met this bug 🐛&lt;/p&gt;




&lt;p&gt;🔍 Fact #4: === Is Fast Because It Refuses to Think&lt;/p&gt;

&lt;p&gt;3 === "3"  // false&lt;/p&gt;

&lt;p&gt;No conversion.&lt;br&gt;
No negotiation.&lt;br&gt;
No surprises.&lt;/p&gt;

&lt;p&gt;This makes ===:&lt;/p&gt;

&lt;p&gt;More predictable&lt;/p&gt;

&lt;p&gt;Easier to debug&lt;/p&gt;

&lt;p&gt;Safer in large codebases&lt;/p&gt;

&lt;p&gt;That’s why linters scream when you use ==.&lt;/p&gt;




&lt;p&gt;🔍 Fact #5: 3.0 === 3 Works (But 0.1 + 0.2 === 0.3 Doesn’t)&lt;/p&gt;

&lt;p&gt;3.0 === 3   // true&lt;br&gt;
0.1 + 0.2 === 0.3 // false&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;JavaScript has one number type&lt;/p&gt;

&lt;p&gt;Integers are exact&lt;/p&gt;

&lt;p&gt;Decimals are approximations (IEEE-754)&lt;/p&gt;

&lt;p&gt;Not a JS bug.&lt;br&gt;
It’s how computers count.&lt;/p&gt;




&lt;p&gt;🧠 Senior-Level Rule of Thumb&lt;/p&gt;

&lt;p&gt;✔ Use === everywhere&lt;br&gt;
✔ Use == only when you understand the coercion rule&lt;br&gt;
❌ Never rely on “JS will figure it out”&lt;/p&gt;




&lt;p&gt;🎯 Interview-Ready Summary&lt;/p&gt;

&lt;p&gt;==   // value comparison + coercion&lt;br&gt;
===  // value + type comparison&lt;/p&gt;

&lt;p&gt;JavaScript is not weird.&lt;br&gt;
It’s extremely precise about very strange rules.&lt;/p&gt;

&lt;p&gt;If this taught you something new, hit 👍&lt;br&gt;
If it surprised you, comment “🤯”&lt;br&gt;
Let’s expose JavaScript’s hidden contracts.&lt;/p&gt;

&lt;h1&gt;
  
  
  JavaScript #WebDevelopment #MERN #Frontend #CodingTips #DeveloperLife #JSFacts
&lt;/h1&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Local Storage vs Cookies for JWT Auth in MERN</title>
      <dc:creator>mayank sagar</dc:creator>
      <pubDate>Mon, 05 Jan 2026 05:29:42 +0000</pubDate>
      <link>https://forem.com/mayankrsagar/local-storage-vs-cookies-for-jwt-auth-in-mern-e14</link>
      <guid>https://forem.com/mayankrsagar/local-storage-vs-cookies-for-jwt-auth-in-mern-e14</guid>
      <description>&lt;p&gt;When building authentication in a MERN application, one of the most common questions is:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where should I store my JWT token?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Most developers end up choosing between:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Local Storage&lt;/li&gt;
&lt;li&gt;HTTP-only Cookies&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both work — but they come with very different security trade-offs.&lt;/p&gt;

&lt;p&gt;This post breaks down &lt;strong&gt;when to use what&lt;/strong&gt;, and &lt;strong&gt;why it matters in real-world apps&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is a JWT (quick recap)
&lt;/h2&gt;

&lt;p&gt;A JSON Web Token (JWT) is commonly used to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Authenticate users&lt;/li&gt;
&lt;li&gt;Maintain sessions in stateless APIs&lt;/li&gt;
&lt;li&gt;Secure routes between frontend and backend&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The real question isn’t &lt;em&gt;whether&lt;/em&gt; to use JWT — it’s &lt;strong&gt;where to store it safely&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Option 1: Storing JWT in Local Storage
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why developers like it
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Very easy to implement&lt;/li&gt;
&lt;li&gt;Simple to access in frontend code&lt;/li&gt;
&lt;li&gt;Works well for quick prototypes
&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="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;token&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;jwtToken&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The problem
&lt;/h3&gt;

&lt;p&gt;Tokens stored in Local Storage are &lt;strong&gt;accessible via JavaScript&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If your app has an XSS vulnerability&lt;/li&gt;
&lt;li&gt;An attacker can read the token&lt;/li&gt;
&lt;li&gt;And impersonate the user&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simple&lt;/li&gt;
&lt;li&gt;Fast to implement&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Vulnerable to XSS&lt;/li&gt;
&lt;li&gt;Not recommended for sensitive production apps&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Option 2: Storing JWT in HTTP-only Cookies
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why professionals prefer it
&lt;/h3&gt;

&lt;p&gt;HTTP-only cookies &lt;strong&gt;cannot be accessed via JavaScript&lt;/strong&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="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cookie&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;token&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;jwtToken&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;httpOnly&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;secure&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;sameSite&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;strict&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Benefits
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Protects against XSS attacks&lt;/li&gt;
&lt;li&gt;Browser automatically sends cookies with requests&lt;/li&gt;
&lt;li&gt;Widely used in production systems&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The trade-off
&lt;/h3&gt;

&lt;p&gt;Cookies can be vulnerable to &lt;strong&gt;CSRF attacks&lt;/strong&gt;, which means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You must use &lt;code&gt;sameSite&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Or implement CSRF tokens&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Safer against XSS&lt;/li&gt;
&lt;li&gt;Production-ready&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Slightly more setup&lt;/li&gt;
&lt;li&gt;Requires CSRF awareness&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Security Comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concern&lt;/th&gt;
&lt;th&gt;Local Storage&lt;/th&gt;
&lt;th&gt;HTTP-only Cookies&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;XSS Protection&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CSRF Risk&lt;/td&gt;
&lt;td&gt;✅ Low&lt;/td&gt;
&lt;td&gt;❌ Needs handling&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;JS Access&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Production Use&lt;/td&gt;
&lt;td&gt;⚠️ Limited&lt;/td&gt;
&lt;td&gt;✅ Recommended&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  What do real-world apps use?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Side projects / demos&lt;/strong&gt; → Local Storage is acceptable&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Production apps&lt;/strong&gt; → HTTP-only Cookies + CSRF protection&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;FinTech / SaaS&lt;/strong&gt; → Cookies almost always&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Security-sensitive apps prioritize &lt;strong&gt;risk reduction over convenience&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Interview-ready answer (save this)
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;“I prefer HTTP-only cookies for JWT storage because they protect against XSS. I handle CSRF using sameSite or CSRF tokens.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Simple, honest, and realistic.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;There’s no “one-size-fits-all” solution — but for &lt;strong&gt;production MERN apps&lt;/strong&gt;, HTTP-only cookies are usually the safer choice.&lt;/p&gt;

&lt;p&gt;If you’re just starting out, Local Storage helps you learn faster.&lt;br&gt;
If users and data matter, &lt;strong&gt;cookies are worth the effort&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Let’s discuss
&lt;/h3&gt;

&lt;p&gt;How do you store JWTs in your apps — and why?&lt;/p&gt;




&lt;p&gt;&lt;code&gt;#mern&lt;/code&gt; &lt;code&gt;#jwt&lt;/code&gt; &lt;code&gt;#authentication&lt;/code&gt; &lt;code&gt;#websecurity&lt;/code&gt; &lt;code&gt;#nodejs&lt;/code&gt; &lt;code&gt;#react&lt;/code&gt;&lt;/p&gt;

</description>
      <category>mern</category>
      <category>websecurity</category>
      <category>authentication</category>
      <category>jwt</category>
    </item>
    <item>
      <title>Master React State Like a Samurai — Past vs Present 🥋⚛️#hooks</title>
      <dc:creator>mayank sagar</dc:creator>
      <pubDate>Mon, 22 Dec 2025 11:45:42 +0000</pubDate>
      <link>https://forem.com/mayankrsagar/master-react-state-like-a-samurai-past-vs-present-hooks-20p7</link>
      <guid>https://forem.com/mayankrsagar/master-react-state-like-a-samurai-past-vs-present-hooks-20p7</guid>
      <description>&lt;p&gt;&lt;strong&gt;Short version:&lt;/strong&gt; most React bugs aren’t logic bugs — they’re &lt;em&gt;time&lt;/em&gt; bugs. You update state, you expect a result, and React gives you a snapshot instead of live reality. Learn three stances to handle it: snapshot updates, functional updates, and cross-render comparison (useRef / usePrevious). This article explains those patterns with clean mental models and small code snippets so you can stop fighting React and start working &lt;em&gt;with&lt;/em&gt; it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why a samurai?
&lt;/h2&gt;

&lt;p&gt;A samurai trains stances to match forces that come at them.&lt;br&gt;
React gives you a snapshot of a component at render time — it’s a frozen stance. If you try to act like it’s “live” you’ll get stale behavior. Think in stances and timing, not brute-force updates.&lt;/p&gt;


&lt;h2&gt;
  
  
  TL;DR (read this first)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;setState(value)&lt;/code&gt; uses the snapshot you read in that render → can be stale.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setState(prev =&amp;gt; next)&lt;/code&gt; uses React’s queued/most-current value → reliable for sequential updates.&lt;/li&gt;
&lt;li&gt;To compare &lt;em&gt;previous&lt;/em&gt; vs &lt;em&gt;current&lt;/em&gt; values across renders, use &lt;code&gt;useRef&lt;/code&gt; (a tiny &lt;code&gt;usePrevious&lt;/code&gt; hook) — it stores the last render’s value without forcing re-renders.&lt;/li&gt;
&lt;/ul&gt;


&lt;h1&gt;
  
  
  1) The battlefield: renders = snapshots
&lt;/h1&gt;

&lt;p&gt;When a component renders, React provides you a snapshot of props and state at that render. That snapshot is immutable for the duration of that render. If you call the updater with &lt;code&gt;setState(count + 1)&lt;/code&gt; multiple times in the same tick, every call sees the same &lt;code&gt;count&lt;/code&gt; value that existed at the start of the render. That’s why the result can be surprising.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Mental model: a render is a photograph. If you update three times inside the same frame and you always read from the photograph, you’ll increment the same number three times on paper — not in the next frame.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;h1&gt;
  
  
  2) The snapshot trap (what goes wrong)
&lt;/h1&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;handleAdd&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="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="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="p"&gt;};&lt;/span&gt;
&lt;span class="c1"&gt;// Expected: 3&lt;/span&gt;
&lt;span class="c1"&gt;// Result: 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Why:&lt;/strong&gt; &lt;code&gt;count&lt;/code&gt; here is the captured value from the render when &lt;code&gt;handleAdd&lt;/code&gt; was created. React batches the updates and applies them on top of the same starting snapshot (unless you use function updaters).&lt;/p&gt;


&lt;h1&gt;
  
  
  3) The functional defense (the stance that never misses)
&lt;/h1&gt;

&lt;p&gt;Use the updater form so each update reads the latest pending state:&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;handleAdd&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="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prev&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;prev&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prev&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;prev&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prev&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="c1"&gt;// Result: 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why this works&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;prev&lt;/code&gt; is provided by React as the most recent state (including queued updates).&lt;/li&gt;
&lt;li&gt;Each updater is applied in order, so updates are sequential and reliable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When to use&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Any time your new state depends on the previous state (increments, toggles, push-to-array, etc.).&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  4) The tracker’s challenge — detecting &lt;em&gt;change&lt;/em&gt;
&lt;/h1&gt;

&lt;p&gt;Sometimes you want to run an effect &lt;em&gt;only when&lt;/em&gt; a specific value changed from its previous render value.&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="nf"&gt;useEffect&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="nf"&gt;fetchUserData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentUserId&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;span class="nx"&gt;currentUserId&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This runs whenever &lt;code&gt;currentUserId&lt;/code&gt; changes — but inside the effect you only see the &lt;em&gt;current&lt;/em&gt; value, not the previous one. &lt;code&gt;useEffect&lt;/code&gt; tells you &lt;em&gt;something changed&lt;/em&gt;, not what it changed &lt;em&gt;from&lt;/em&gt;. If you need to know whether &lt;code&gt;currentUserId&lt;/code&gt; changed from a particular previous value (or changed at all compared to last render), you must keep the previous value yourself.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Mental model: you see a fresh footprint in mud — you don’t automatically know who made it unless you kept the old print to compare with.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  5) The usePrevious scroll — time-travel without breaking React
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;useRef&lt;/code&gt; stores a value across renders without triggering re-renders. Combine it with &lt;code&gt;useEffect&lt;/code&gt; and you get a tiny &lt;code&gt;usePrevious&lt;/code&gt; hook:&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;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;usePrevious&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&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;ref&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nf"&gt;useEffect&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="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// update AFTER render&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// the previous render's value&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;Usage&lt;/strong&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;prevUserId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;usePrevious&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentUserId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;useEffect&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevUserId&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;currentUserId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// userId just changed — do something&lt;/span&gt;
    &lt;span class="nf"&gt;fetchUserData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentUserId&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;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;currentUserId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;prevUserId&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;Why this works&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;ref.current&lt;/code&gt; persists across renders but updating it doesn't cause a re-render.&lt;/li&gt;
&lt;li&gt;Because the effect that writes &lt;code&gt;ref.current&lt;/code&gt; runs after render, the value you read from &lt;code&gt;usePrevious&lt;/code&gt; is the previous render’s value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Mental model:&lt;/strong&gt; the sensei keeps a scroll of yesterday’s note. He can compare it to today’s situation without shouting and reconfiguring the dojos.&lt;/p&gt;




&lt;h1&gt;
  
  
  6) Master the stances — short checklist
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Snapshot update:&lt;/strong&gt; &lt;code&gt;setState(newValue)&lt;/code&gt;&lt;br&gt;
Use when you replace state with a value that doesn’t depend on the previous state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Sequential update:&lt;/strong&gt; &lt;code&gt;setState(prev =&amp;gt; next)&lt;/code&gt;&lt;br&gt;
Use when next depends on prev (increments, toggles, array pushes). This is your defensive stance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Cross-render comparison:&lt;/strong&gt; &lt;code&gt;useRef&lt;/code&gt; / &lt;code&gt;usePrevious&lt;/code&gt;&lt;br&gt;
Use when you must detect transitions — "did X change from Y to Z?"&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Practical examples &amp;amp; gotchas
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Updating arrays safely
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;setTodos&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;newTodo&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// use prev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Toggling reliably
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;setOpen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Avoid trying to read "latest" state synchronously after &lt;code&gt;setState&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;setState&lt;/code&gt; is asynchronous; don't assume state was updated immediately after the call. Use effects or callbacks where necessary.&lt;/p&gt;




&lt;h1&gt;
  
  
  Final thought
&lt;/h1&gt;

&lt;p&gt;Most React headaches are &lt;em&gt;about time&lt;/em&gt;, not logic. Think in renders (snapshots), not in instant live values. When you shift to the right stance — snapshot, functional, or cross-render memory — your code becomes calmer, predictable, and sharper.&lt;/p&gt;

&lt;p&gt;Which stance are you practicing today? ⚔️🧭📜&lt;/p&gt;




</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>frontend</category>
    </item>
    <item>
      <title>React: The Battle of Implicit vs. Explicit Returns. Which side are you on?</title>
      <dc:creator>mayank sagar</dc:creator>
      <pubDate>Fri, 12 Dec 2025 12:50:37 +0000</pubDate>
      <link>https://forem.com/mayankrsagar/react-the-battle-of-implicit-vs-explicit-returns-which-side-are-you-on-43k4</link>
      <guid>https://forem.com/mayankrsagar/react-the-battle-of-implicit-vs-explicit-returns-which-side-are-you-on-43k4</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;I was reviewing PRs this morning and found myself making the same syntax nitpick for the third time this week.

It brought up a constant internal battle I have when writing modern React: &lt;span class="gs"&gt;**Implicit vs. Explicit returns in arrow functions.**&lt;/span&gt;

We all love arrow functions. They transformed clunky class components into sleek functional components. But in the pursuit of "sleekness," are we sometimes sacrificing readability?

I want to hear where the community draws the line.

&lt;span class="gu"&gt;## The Contenders&lt;/span&gt;

Quick refresher on the two styles we're fighting over.

&lt;span class="gu"&gt;### 1. The Implicit Return (The "Sleek" one)&lt;/span&gt;

If your component does nothing but dump props straight into JSX, you can ditch the curly braces &lt;span class="sb"&gt;`{}`&lt;/span&gt; and the &lt;span class="sb"&gt;`return`&lt;/span&gt; keyword entirely. You usually wrap the JSX in parentheses &lt;span class="sb"&gt;`()`&lt;/span&gt;.

It looks neat, tidy, and very "functional."

&lt;span class="p"&gt;```&lt;/span&gt;&lt;span class="nl"&gt;
&lt;/span&gt;
jsx
// Look ma, no curly braces!
// Very clean for simple presentational components.
const UserCard = ({ username, avatarUrl }) =&amp;gt; (
  &amp;lt;div className="card"&amp;gt;
    &amp;lt;img src={avatarUrl} alt={username} /&amp;gt;
    &amp;lt;h3&amp;gt;{username}&amp;lt;/h3&amp;gt;
  &amp;lt;/div&amp;gt;
);


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  2. The Explicit Return (The "Safe" one)
&lt;/h3&gt;

&lt;p&gt;This uses the standard block body syntax with curly braces &lt;code&gt;{}&lt;/code&gt;. Because you opened the braces, you &lt;em&gt;must&lt;/em&gt; type the &lt;code&gt;return&lt;/code&gt; keyword to render anything.&lt;/p&gt;

&lt;p&gt;You are forced to use this if you have &lt;em&gt;any&lt;/em&gt; logic, hooks, or variable declarations before rendering.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
jsx
const UserCard = ({ user }) =&amp;gt; {
  // We have logic before rendering...
  const displayName = user.name.toUpperCase();
  const isPremium = user.tier === 'gold';

  // ...so we MUST be explicit with the return.
  return (
    &amp;lt;div className={isPremium ? 'gold-card' : 'standard-card'}&amp;gt;
      &amp;lt;h3&amp;gt;{displayName}&amp;lt;/h3&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;h2&gt;
  
  
  The Grey Area (Where the debate lies)
&lt;/h2&gt;

&lt;p&gt;Nobody argues about the examples above. The problem is the grey area.&lt;/p&gt;

&lt;h3&gt;
  
  
  The "Habitual Explicit"
&lt;/h3&gt;

&lt;p&gt;Sometimes I see developers using explicit returns out of sheer habit, even when there is zero logic before the return. It adds three extra lines of boilerplate code (the braces and the return keyword) for no functional reason.&lt;/p&gt;

&lt;h3&gt;
  
  
  The "Tortured Implicit"
&lt;/h3&gt;

&lt;p&gt;Conversely, I often see developers trying &lt;em&gt;way&lt;/em&gt; too hard to keep the "sleek" implicit syntax. They end up nesting ternary operators four levels deep inside the JSX just to avoid opening up curly braces and writing a messy &lt;code&gt;if/else&lt;/code&gt; block before the return.&lt;/p&gt;

&lt;p&gt;This might look "cooler," but it's a nightmare to read 6 months later.&lt;/p&gt;

&lt;h3&gt;
  
  
  The "Gotcha" Trap ⚠️
&lt;/h3&gt;

&lt;p&gt;And let's not forget the classic rookie mistake. If you try to implicitly return an object literal (common in utility functions or older Redux &lt;code&gt;mapStateToProps&lt;/code&gt;), JavaScript thinks the object braces are function body braces.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
javascript
// ❌ WRONG. Returns undefined. JS gets confused.
const getInitialState = () =&amp;gt; { id: 1, active: true };

// ✅ CORRECT. You must wrap the object in parentheses.
const getInitialState = () =&amp;gt; ({ id: 1, active: true });


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;h2&gt;
  
  
  The Discussion
&lt;/h2&gt;

&lt;p&gt;My general rule of thumb: &lt;strong&gt;Use implicit returns by default for brevity, but switch to explicit the second the JSX becomes hard to scan on a single pass.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Readability &amp;gt; Brevity. Every time.&lt;/p&gt;

&lt;p&gt;But I know many teams enforce strict linting rules one way or the other.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What about you?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Are you Team Implicit (keep it short) or Team Explicit (keep it obvious)?&lt;/li&gt;
&lt;li&gt; Do you feel that implicit returns make code harder to debug?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's debate syntax in the comments! 👇&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>discuss</category>
    </item>
    <item>
      <title>'JS &amp; React "Silent Killers": Implicit Returns &amp; Stale State'</title>
      <dc:creator>mayank sagar</dc:creator>
      <pubDate>Wed, 10 Dec 2025 06:50:09 +0000</pubDate>
      <link>https://forem.com/mayankrsagar/js-react-silent-killers-implicit-returns-stale-state-1oeg</link>
      <guid>https://forem.com/mayankrsagar/js-react-silent-killers-implicit-returns-stale-state-1oeg</guid>
      <description>&lt;p&gt;&lt;strong&gt;Description&lt;/strong&gt;: Two fundamental concepts that trip up developers moving fast: Arrow function curly braces and asynchronous React state updates. Master them today.&lt;/p&gt;

&lt;p&gt;We've all been there. You're moving fast, building out a feature, and everything seems to be working fine. Then, you introduce a slight complexity—maybe an extra line of logic in a function or a second state update—and suddenly things break silently.&lt;/p&gt;

&lt;p&gt;Your data comes back &lt;code&gt;undefined&lt;/code&gt;. Your counters aren't counting correctly.&lt;/p&gt;

&lt;p&gt;When moving from vanilla JavaScript to modern React development, it's easy to rely on syntax that "just seems to work" without fully grasping the mechanics underneath.&lt;/p&gt;

&lt;p&gt;Today, we're going back to basics to master two concepts that are responsible for a surprisingly high number of bugs in frontend codebases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;JavaScript:&lt;/strong&gt; Implicit vs. Explicit Arrow Function Returns.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;React:&lt;/strong&gt; Stale vs. Previous State Updates.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's fix these mental models once and for all.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. The JavaScript Trap: Implicit vs. Explicit Returns
&lt;/h2&gt;

&lt;p&gt;JavaScript arrow functions &lt;code&gt;() =&amp;gt; ...&lt;/code&gt; are fantastic syntactic sugar. They make functional programming patterns like map, filter, and reduce look incredibly clean.&lt;/p&gt;

&lt;p&gt;But that cleanliness hides a common trap related to curly braces &lt;code&gt;{}&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The "Magic" One-Liner (Implicit Return)
&lt;/h3&gt;

&lt;p&gt;When an arrow function fits on one line and &lt;strong&gt;does not&lt;/strong&gt; use curly braces, JavaScript automatically assumes you want to return the result of that expression. This is called an "implicit return."&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="c1"&gt;// ✅ Clean and simple.&lt;/span&gt;
&lt;span class="c1"&gt;// The result of 'a + b' is automatically returned.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&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="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Output: 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is great, until you need to add just &lt;em&gt;one more line&lt;/em&gt; of code to that function, perhaps for debugging.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Silent Mistake
&lt;/h3&gt;

&lt;p&gt;The moment you wrap the function body in curly braces &lt;code&gt;{}&lt;/code&gt; to add more logic, you have changed the rules. You have created a &lt;strong&gt;block scope&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Inside a block scope, the "magic" implicit return stops working. If you don't explicitly tell JavaScript to return a value, functions return &lt;code&gt;undefined&lt;/code&gt; by default.&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="c1"&gt;// ❌ THE TRAP&lt;/span&gt;
&lt;span class="c1"&gt;// We added braces to perhaps add a console.log later.&lt;/span&gt;
&lt;span class="c1"&gt;// But now, this function does nothing.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&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="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&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="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Output: undefined 😱&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have seen senior developers stare at code like this for 20 minutes wondering why their data pipeline is breaking downstream.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Fix (Explicit Return)
&lt;/h3&gt;

&lt;p&gt;If you use the curly braces, you &lt;strong&gt;must&lt;/strong&gt; use the &lt;code&gt;return&lt;/code&gt; keyword.&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="c1"&gt;// ✅ The Fix&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Calculating...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Explicitly giving the value back&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="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Output: 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Rule of Thumb:&lt;/strong&gt; No braces? It returns automatically. Braces? You gotta type &lt;code&gt;return&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. The React Trap: Stale State vs. Previous State
&lt;/h2&gt;

&lt;p&gt;Now let's move to React. A massive mental hurdle for developers is realizing that &lt;strong&gt;state updates are asynchronous and batched.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you call a state setter function (like &lt;code&gt;setCount&lt;/code&gt;), React doesn't update the variable immediately. It schedules an update for the next render cycle.&lt;/p&gt;

&lt;h3&gt;
  
  
  The "Normal" Way (Risky)
&lt;/h3&gt;

&lt;p&gt;If your new state depends on the &lt;em&gt;current&lt;/em&gt; state, using the standard state variable is dangerous if updates happen rapidly (e.g., fast button clicks, loops, or multiple &lt;code&gt;useEffect&lt;/code&gt; triggers).&lt;/p&gt;

&lt;p&gt;Because of closures in JavaScript, the function often "captures" a stale version of the variable.&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&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="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Imagine count is currently 0.&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;buggyIncrement&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="c1"&gt;// Line 1: React schedules count to become 0 + 1&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="c1"&gt;// Line 2: Because the re-render hasn't happened yet, &lt;/span&gt;
    &lt;span class="c1"&gt;// 'count' is STILL 0 here.&lt;/span&gt;
    &lt;span class="c1"&gt;// React schedules count to become 0 + 1 again.&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="p"&gt;};&lt;/span&gt;

  &lt;span class="c1"&gt;// Result after re-render: count is 1, not 2. We lost an update.&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;buggyIncrement&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The Fix: The "Updater Callback"
&lt;/h3&gt;

&lt;p&gt;React provides a solution. Instead of passing a raw value to the setter, you should pass a &lt;strong&gt;callback function&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;React guarantees that the argument passed to this callback function will always be the &lt;strong&gt;most current, pending state&lt;/strong&gt; sitting in the update queue, regardless of when the component last rendered.&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&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="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&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;safeIncrement&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="c1"&gt;// ✅ React ensures 'prevCount' is the freshest value.&lt;/span&gt;

    &lt;span class="c1"&gt;// If count is 0, 'prevCount' here is 0.&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;prevCount&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prevCount&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="c1"&gt;// Even though a re-render hasn't happened, React knows &lt;/span&gt;
    &lt;span class="c1"&gt;// there is a pending update. 'prevCount' here is now 1.&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;prevCount&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prevCount&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="c1"&gt;// Result after re-render: count is safely 2.&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;safeIncrement&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;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;Rule of Thumb:&lt;/strong&gt; If your new state depends on the old state (counters, toggles, appending to arrays), &lt;em&gt;always&lt;/em&gt; use the callback pattern: &lt;code&gt;setSomething(prev =&amp;gt; ...)&lt;/code&gt;&lt;/p&gt;




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

&lt;p&gt;These concepts seem small, but they are the root cause of many race conditions and "ghost bugs" in frontend applications.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Don't let arrow function curly braces swallow your return values.&lt;/li&gt;
&lt;li&gt; Don't trust state variables during rapid updates; trust the updater callback.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By mastering these fundamental behaviors, you stop guessing why your code works and start knowing exactly how it executes.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Which one of these concepts gave you the hardest time when you were learning? Let me know in the comments below!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to Fix CORS Errors in MERN (The Secure Way)</title>
      <dc:creator>mayank sagar</dc:creator>
      <pubDate>Tue, 09 Dec 2025 06:08:43 +0000</pubDate>
      <link>https://forem.com/mayankrsagar/how-to-fix-cors-errors-in-mern-the-secure-way-4c1k</link>
      <guid>https://forem.com/mayankrsagar/how-to-fix-cors-errors-in-mern-the-secure-way-4c1k</guid>
      <description>&lt;h1&gt;
  
  
  Stop Struggling with CORS Errors in the MERN Stack (A Visual Guide)
&lt;/h1&gt;

&lt;p&gt;It’s a rite of passage for every full-stack developer.&lt;/p&gt;

&lt;p&gt;You spend hours building your Express backend logic. You test every API endpoint precisely in Postman, and you get that satisfying green &lt;code&gt;200 OK&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You finally connect your React frontend, hit the button to fetch data, and BAM. The browser console screams at you in red text.&lt;/p&gt;

&lt;p&gt;❌ &lt;em&gt;Access to fetch at '&lt;a href="http://localhost:5000" rel="noopener noreferrer"&gt;http://localhost:5000&lt;/a&gt;' from origin '&lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;' has been blocked by CORS policy.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you are staring at that error right now, don't panic. It’s frustrating, but it’s actually a good thing. Here is exactly why it happens and the secure way to fix it in less than 2 minutes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is this happening?
&lt;/h2&gt;

&lt;p&gt;It’s not a bug; it’s a feature.&lt;/p&gt;

&lt;p&gt;Browsers have a built-in security mechanism called the &lt;strong&gt;Same-Origin Policy&lt;/strong&gt;. Its job is to prevent malicious scripts on one webpage from obtaining access to sensitive data on another webpage.&lt;/p&gt;

&lt;p&gt;In a typical local MERN stack setup:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your React frontend is running on &lt;code&gt;http://localhost:3000&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Your Node/Express backend is running on &lt;code&gt;http://localhost:5000&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To the browser, these different ports make them completely separate "origins." The browser doesn't know that you own both of them. It sees a stranger trying to talk to your server, and it blocks the request to protect you.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Fix: Whitelist Your Frontend
&lt;/h2&gt;

&lt;p&gt;To fix this, we need to tell our Express server, "Hey, it's okay. That frontend over on Port 3000 is with me."&lt;/p&gt;

&lt;p&gt;We do this using Cross-Origin Resource Sharing (CORS) headers. The easiest way to manage this in Node.js is with the &lt;code&gt;cors&lt;/code&gt; middleware package.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Install the package
&lt;/h3&gt;

&lt;p&gt;Navigate to your &lt;strong&gt;backend&lt;/strong&gt; (server) folder in your terminal and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;cors
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Configure the server
&lt;/h3&gt;

&lt;p&gt;Open your main server file (usually &lt;code&gt;index.js&lt;/code&gt; or &lt;code&gt;server.js&lt;/code&gt;). You need to import &lt;code&gt;cors&lt;/code&gt; and use it &lt;em&gt;before&lt;/em&gt; your routes are defined.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here is the code:&lt;/strong&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&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;cors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cors&lt;/span&gt;&lt;span class="dl"&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;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// The Magic Lines 👇&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://localhost:3000&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Whitelist YOUR frontend only&lt;/span&gt;
  &lt;span class="na"&gt;credentials&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="c1"&gt;// Required if you use cookies/sessions&lt;/span&gt;
&lt;span class="p"&gt;}));&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="c1"&gt;// Your routes go here...&lt;/span&gt;
&lt;span class="c1"&gt;// app.use('/api/users', userRoutes);&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5000&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="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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Server running on port 5000&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By setting the &lt;code&gt;origin&lt;/code&gt; to &lt;code&gt;http://localhost:3000&lt;/code&gt;, the server will now send the correct headers back to the browser, and the browser will allow the data through.&lt;/p&gt;

&lt;h2&gt;
  
  
  ⚠️ The Critical Production Warning
&lt;/h2&gt;

&lt;p&gt;When you are debugging late at night, you might find StackOverflow answers telling you to just use this:&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="c1"&gt;// DANGER ZONE&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While this &lt;em&gt;does&lt;/em&gt; fix the error, it does so by allowing &lt;strong&gt;any website on the internet&lt;/strong&gt; to make requests to your API.&lt;/p&gt;

&lt;p&gt;This is fine for a quick local test, but &lt;strong&gt;never push this to production&lt;/strong&gt;. It opens a massive security hole.&lt;/p&gt;

&lt;p&gt;In a production environment, always use an environment variable for your frontend URL to keep it secure:&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="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FRONTEND_URL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// e.g., 'https://myapp.com'&lt;/span&gt;
&lt;span class="p"&gt;}));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;CORS is annoying, but it keeps the web safer.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; It happens because your frontend and backend are on different ports/domains.&lt;/li&gt;
&lt;li&gt; Fix it by installing &lt;code&gt;cors&lt;/code&gt; on your backend.&lt;/li&gt;
&lt;li&gt; Always whitelist your specific frontend origin; don't open it to everyone.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Did this guide save you a headache? Let me know in the comments!👇&lt;/p&gt;




&lt;p&gt;&lt;em&gt;If you found this helpful, let's connect on &lt;a href="https://www.linkedin.com/in/mayank-sagar-mern/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://x.com/mayankrsagar" rel="noopener noreferrer"&gt;X (Twitter)&lt;/a&gt; where I share more visual guides on full-stack development.&lt;/em&gt;&lt;/p&gt;




</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>node</category>
    </item>
  </channel>
</rss>
