<?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: Bushra</title>
    <description>The latest articles on Forem by Bushra (@bushra_sayyed).</description>
    <link>https://forem.com/bushra_sayyed</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%2F3318848%2Fd80e7d6f-efed-4840-ba50-165b6e65aa9c.png</url>
      <title>Forem: Bushra</title>
      <link>https://forem.com/bushra_sayyed</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/bushra_sayyed"/>
    <language>en</language>
    <item>
      <title>Next.js Hydration Error: Why It Happens &amp; How to Fix It (For Good)</title>
      <dc:creator>Bushra</dc:creator>
      <pubDate>Mon, 15 Sep 2025 16:01:14 +0000</pubDate>
      <link>https://forem.com/bushra_sayyed/nextjs-hydration-error-why-it-happens-how-to-fix-it-for-good-29m9</link>
      <guid>https://forem.com/bushra_sayyed/nextjs-hydration-error-why-it-happens-how-to-fix-it-for-good-29m9</guid>
      <description>&lt;p&gt;If you’ve worked with &lt;strong&gt;Next.js&lt;/strong&gt;, you’ve probably seen this warning pop up at least once:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Warning: Text content does not match server-rendered HTML.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;infamous Hydration Error.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The first time I saw it, I thought: &lt;em&gt;“Wait, my app works… so why is this error screaming at me?”&lt;/em&gt;&lt;br&gt;
And then, project after project, it kept showing up.&lt;/p&gt;

&lt;p&gt;Turns out, it’s one of the &lt;strong&gt;most common hurdles for anyone moving from React to Next.js.&lt;/strong&gt;&lt;br&gt;
And the good news? Once you understand the &lt;em&gt;why&lt;/em&gt;, fixing it becomes straightforward.&lt;/p&gt;


&lt;h2&gt;
  
  
  🔎 Why does this happen?
&lt;/h2&gt;

&lt;p&gt;Hydration = connecting &lt;strong&gt;server-rendered HTML&lt;/strong&gt; with &lt;strong&gt;React on the client&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If what the server outputs doesn’t exactly match what React expects → hydration error.&lt;br&gt;
It’s not random. It’s just Next.js saying: &lt;em&gt;“Hey, something’s different here.”&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Usual suspects I faced:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Using &lt;code&gt;window&lt;/code&gt; / &lt;code&gt;document&lt;/code&gt; on the server (which doesn’t exist in SSR)&lt;/li&gt;
&lt;li&gt;Values that change between renders (&lt;code&gt;Math.random()&lt;/code&gt;, &lt;code&gt;Date.now()&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Conditional rendering behaving differently on server vs client&lt;/li&gt;
&lt;li&gt;Third-party components that aren’t SSR-safe&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  ✅ How I fixed it
&lt;/h2&gt;

&lt;p&gt;Here are the solutions that worked every time:&lt;/p&gt;

&lt;p&gt;1.&lt;strong&gt;Run browser-only code in &lt;code&gt;useEffect&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;   &lt;span class="c1"&gt;// ❌ Wrong&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerWidth&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="c1"&gt;// ✅ Correct&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;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setWidth&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&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="nf"&gt;setWidth&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerWidth&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;2.&lt;strong&gt;Use dynamic imports for client-only components&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;   &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;dynamic&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;next/dynamic&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;Chart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dynamic&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../components/Chart&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="na"&gt;ssr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.&lt;strong&gt;Keep initial render consistent&lt;/strong&gt;&lt;br&gt;
   If you show placeholders/spinners, make sure they render the same on both server and client before React takes over.&lt;/p&gt;

&lt;p&gt;4.&lt;strong&gt;Check your libraries&lt;/strong&gt;&lt;br&gt;
   Not every npm package is built with SSR in mind. Use alternatives or load them dynamically.&lt;/p&gt;

&lt;p&gt;5.&lt;strong&gt;Debug smart&lt;/strong&gt;&lt;br&gt;
   Compare what’s rendered on the server vs client — that mismatch is where the error starts.&lt;/p&gt;




&lt;h2&gt;
  
  
  🚀 Best practices to avoid hydration errors
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Predictable rendering only.&lt;/strong&gt; Don’t use values that change between renders.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Separate SSR-safe vs client-only logic early.&lt;/strong&gt; Think: &lt;em&gt;Can this run on the server?&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Leverage &lt;code&gt;useEffect&lt;/code&gt; &amp;amp; dynamic imports.&lt;/strong&gt; These are your go-to tools.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Be cautious with conditional rendering.&lt;/strong&gt; Both server &amp;amp; client must agree on the initial output.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test with production builds (&lt;code&gt;next build &amp;amp;&amp;amp; next start&lt;/code&gt;).&lt;/strong&gt; Some hydration issues don’t show in dev mode.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  💡 Takeaway
&lt;/h2&gt;

&lt;p&gt;Hydration errors aren’t random bugs. They’re actually signals telling you something isn’t SSR-friendly.&lt;/p&gt;

&lt;p&gt;Once you understand them, you’ll find your Next.js apps become more stable, predictable, and production-ready.&lt;/p&gt;




&lt;h2&gt;
  
  
  💬 Over to you
&lt;/h2&gt;

&lt;p&gt;Have hydration errors ever driven you crazy?&lt;br&gt;
How did you fix them? Or maybe… are you still fighting with them right now? 😅&lt;/p&gt;

&lt;p&gt;Drop your story — let’s make this error a little less scary together.&lt;/p&gt;




</description>
      <category>nextjs</category>
      <category>react</category>
      <category>hydration</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
