• Blog
  • Documentation
  • Courses
  • Changelog
  • AI Starters
  • UI Kit
  • FAQ
  • Supamode
    New
  • Pricing

Launch your next SaaS in record time with Makerkit, a React SaaS Boilerplate for Next.js and Supabase.

Makerkit is a product of Makerkit Pte Ltd (registered in the Republic of Singapore)Company Registration No: 202407149CFor support or inquiries, please contact us

About
  • FAQ
  • Contact
  • Verify your Discord
  • Consultation
  • Open Source
  • Become an Affiliate
Product
  • Documentation
  • Blog
  • Changelog
  • UI Blocks
  • Figma UI Kit
  • AI SaaS Starters
License
  • Activate License
  • Upgrade License
  • Invite Member
Legal
  • Terms of License

How to fix common hydration issues in Next.js and React 18

Aug 6, 2022

Common Next.js hydration issues and how to fix them

next
react

Since updating React 18, Next.js has been stricter when it comes to hydrating the client-side after the page was server-rendered initially on the server.

In fact, you likely keep seeing errors such as "Text content does not match server-rendered HTML" when the page loads. It took a while for me to understand it, hoping it was a bug in React; but as it turned out, it was an error on my end.

While upgrading Makerkit to React 18, I found two common issues:

  1. The HTML was invalid - e.g. malformed
  2. Conditional Rendering based on browser vs server conditions

Malformed HTML

Malformed HTML doesn't automatically mean it will render badly as browsers are capable of handling these situations fairly well. As such, it's rather hard being able to locate places where we used a tag in the wrong place, such as in the code below:

tsx
<p>
{ */ Wrong! */ }
<div>
{children}
</div>
<p>

In the example above, p shouldn't be a parent of div. Browsers may be forgiving, but other DOM implementations may not, stripping the internal invalid tags. This is why the server and the browser end up rendering different HTML strings, even when there is no apparent difference between the two.

The fix here is very simple: make sure you write syntactically valid HTML code.

Conditional Rendering

Very often, we only want to display a value on the client instead than on the server. In Makerkit's SaaS boilerplate for Next.js, this issue happens a lot, because we cannot run the Web Firebase SDK on the server.

Therefore, to avoid rendering on the server, we used to do the following checks:

tsx
{ isBrowser() ? <MyComponent /> : null }

And the above still looks fine, but React 18 didn't quite like it. To solve this issue, we used next/dynamic to import the components dynamically and by setting the option ssr to false:

tsx
import dynamic from 'next/dynamic';
const MyComponent = dynamic(() => import(`./MyComponent.tsx`), {
ssr: false
});
// somewhere in your code simply render the component
return <MyComponent />

And this solved the second issue. To summarize, whenever you're using conditions to avoid rendering components on the server with Next.js and React 18, simply use a dynamic import instead.

I hope these two solutions will help you fix your hydration issues. If you have any questions, do contact me!

Some other posts you might like...
Jun 9, 2025Claude Code: Build a SaaS with AIThis is a step-by-step guide to building an AI Content Repurposer SaaS by vibe-coding with Claude Code and Makerkit.
Apr 23, 2025Next.js Security: A Comprehensive Guide how to secure your Next.js applicationA comprehensive guide to securing your Next.js application, focusing on practical strategies to protect your application and user data from common vulnerabilities.
Jan 17, 2025Best Practices for Building a SaaS with Windsurf and MakerkitWindsurf is a new AI-powered editor taking the developer experience to the next level. With the new optimized rules for Makerkit, building a SaaS just got a lot easier!
Jan 16, 2025Best Practices for Building a SaaS with Cursor and MakerkitCursor is the hottest AI editor in the market. With the new optimized rules for Makerkit, building a SaaS just got a lot easier!
Dec 26, 2024Choosing the best hosting provider for your Next.js applicationIn this post, we'll show you how to choose the best hosting provider for your Next.js application.
Dec 25, 2024Updating Shadcn UI Components to React 19In React 19, the 'forwardRef' function was deprecated. This post will show you how to update your Shadcn UI components to work with React 19 and ensure future compatibility.