Fix For - Error: Hydration Failed Because The Initial UI Does Not Match What Was Rendered on The Server

Fix For - Error: Hydration Failed Because The Initial UI Does Not Match What Was Rendered on The Server

3 min read · 442 words · Shared November 3, 2023 by

Article Summary: When upgrading to Next.js version 13, I encountered a hydration error. This is what a hydration error is and how to fix them.

Errors are never fun. I had a project that I was updating to the latest version of Next.js and I got the following error:

Screenshot of the hydration error
View Full Image

·

Screenshot of the hydration error

What's A Hydration Error?

This error is a hydration error. Hydration is the process of taking the HTML that is rendered on the server and turning it into a fully interactive React application in the client. This error is telling us that the HTML that was rendered on the server is different than what was rendered on the client. This is a problem because React will not be able to hydrate the application.

How To Fix A Hydration Error

A hydration error can be caused by a multiple issues which makes this a bit tricky to debug. I was also updating all my packages at once so I wasn't sure which yarn package was actually causing this error.

Luckily, Next.js gives us some info about possible reasons for this error. In my case it was a mismatched <a> tag. In case that wasn't the issue, I also went to the link the suggested. Here I found some more possible reasons:

  1. Incorrect nesting of HTML tags
    1. <p> nested in another <p> tag
  2. Using checks like typeof window !== 'undefined' in your rendering logic
  3. Using browser-only APIs like window or localStorage in your rendering logic
  4. ... and some more reasons

I was able to pinpoint my issue to the upgrade of the Next/Link component. You used to have to wrap your own <a> tag in the Next/Link component. Now you can just use the Next/Link component and it will render the <a> tag for you.

Quick Fix Using Code Mod

Before going through your entire code base and manually finding and fixing each and every nested a tag, it might behove you to know that a codemod is available to fix this issue.

Here is the link to the codemod that will remove <a> tags inside Link Components, or add a legacyBehavior prop to Links that cannot be auto-fixed. Here is an example of what it will do:

<Link href="/about">
  <a>About</a>
</Link>
// transforms into
<Link href="/about">
  About
</Link>
 
<Link href="/about">
  <a onClick={() => console.log('clicked')}>About</a>
</Link>
// transforms into
<Link href="/about" onClick={() => console.log('clicked')}>
  About
</Link>

If you are not able to fix this error, it will still run in production. You will just see an error in the console but it won't break your application.

Edit on GitHub

Related Tags

    #nextjs

Related Posts

Add Algolia's InstantSearch to Next.js - A Quickstart · December 17, 2021
Generate A Dynamic Sitemap In Next.js Website · September 27, 2021
Add Splitbee Analytics To Next.js · July 20, 2021
Add Google Analytics To Next.js Website · April 29, 2021
Connect Firebase To Next.js · April 15, 2021

On This Page

What's A Hydration Error?

How To Fix A Hydration Error

Quick Fix Using Code Mod

View Related Posts

December 17, 2021

algolia.png

Add Algolia's InstantSearch to Next.js - A Quickstart

September 27, 2021

nextjs-light.png

Generate A Dynamic Sitemap In Next.js Website

July 20, 2021

splitbee.png

Add Splitbee Analytics To Next.js

April 29, 2021

google-analytics.png

Add Google Analytics To Next.js Website

April 15, 2021

firebase.png

Connect Firebase To Next.js


Loading author data...

    Legal

    Terms

    Disclaimer

    Privacy Policy


    Carlson Technologies Logo© Copyright 2021 - 2024, Carlson Technologies LLC. All Rights Reserved.