suppressHydrationWarning. What is it?
Tags: javascript, opensource, nextjs, node
Published: 2024-05-28 22:41:36

You might have faced an error like “Hydration failed because the initial UI does not match what was rendered on the server.” So did [shadcn-ui/ui](https://github.com/shadcn-ui/ui/blob/main/apps/www/app/layout.tsx). How can I be so sure? because they have suppressHydrationWarning attribute in the [app/layout.tsx](https://github.com/shadcn-ui/ui/blob/main/apps/www/app/layout.tsx)

I am making efforts to understand how shadcn-ui/ui is built and in this process, I saw suppressHydrationWarning in the app/layout.tsx.

I found this next.js documentation link: [Text content does not match server-rendered HTML](https://nextjs.org/docs/messages/react-hydration-error) and it is quite insightful.

![](https://media.licdn.com/dms/image/D4E12AQHSvZRhR3B1EA/article-inline_image-shrink_1000_1488/0/1716832953872?e=1722470400&v=beta&t=0GMMDpaXyhX_hWnGiCjzEnK0NU8XSJaBpsvEWP3IMDk)

### Why does hydration error occur?

When there is a mismatch between the React tree pre-rendered on server and the first rendered React tree in the browser, in other words, during hydration.

[Hydration](https://react.dev/reference/react-dom/client/hydrateRoot) is when React converts the pre-rendered HTML from the server into a fully interactive application by attaching event handlers.

The following common causes are picked from the Next.js documentation

> [Build shadcn-ui/ui from scratch.](https://tthroo.com/)

### Common Causes

Hydration errors can occur from:

1.  Incorrect nesting of HTML tags

- `<p>` nested in another `<p>` tag
- `<div>` nested in a `<p>` tag
- `<ul>` or `<ol>` nested in a `<p>` tag
- [Interactive Content](https://html.spec.whatwg.org/#interactive-content-2) cannot be nested (`<a>` nested in a `<a>` tag, <button> nested in a `<button>` tag, etc.)

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\. Using time-dependent APIs such as the Date() constructor in your rendering logic

5. [Browser extensions](https://github.com/facebook/react/issues/24430) modifying the HTML

6\. Incorrectly configured [CSS-in-JS libraries](https://nextjs.org/docs/app/building-your-application/styling/css-in-js)

*   Ensure your code is following [our official examples](https://github.com/vercel/next.js/tree/canary/examples)

7\. Incorrectly configured Edge/CDN that attempts to modify the html response, such as Cloudflare [Auto Minify](https://developers.cloudflare.com/speed/optimization/content/auto-minify/)

### Possible solutions:

There are three possible ways to fix the hydration error.

[Solution 1: Using](https://nextjs.org/docs/messages/react-hydration-error#solution-1-using-useeffect-to-run-on-the-client-only) useEffect [to run on the client only](https://nextjs.org/docs/messages/react-hydration-error#solution-1-using-useeffect-to-run-on-the-client-only)

[Solution 2: Disabling SSR on specific components](https://nextjs.org/docs/messages/react-hydration-error#solution-2-disabling-ssr-on-specific-components)

[Solution 3: Using](https://nextjs.org/docs/messages/react-hydration-error#solution-3-using-suppresshydrationwarning) suppressHydrationWarning

This stackoverflow question: [How To Solve React Hydration Error in Next](https://stackoverflow.com/questions/73451295/how-to-solve-react-hydration-error-in-next) was solved by using the appropriate tbody tag in the table. Although #1 in [common causes](https://nextjs.org/docs/messages/react-hydration-error#common-causes) does not specifically point out that for table tag, hydration error might occur if you skip adding tbody, it is worth considering.

See [Suppress Hydration Warning (React Docs)](https://reactjs.org/docs/dom-elements.html#suppresshydrationwarning):

> _If you set suppressHydrationWarning to true, React will not warn you about mismatches in the attributes and the content of that element. It only works one level deep, and is intended to be used as an escape hatch. Don’t overuse it. You can read more about hydration in the ReactDOM.hydrateRoot() documentation._

### Conclusion:

Use suppressHydrationWarning as your last resort if you cannot resolve the issue by referring to common causes.

I said last resort because consider this sentence from the Next.js docs — “Sometimes content will inevitably differ between the server and client, such as a timestamp. You can silence the hydration mismatch warning by adding suppressHydrationWarning={true} to the element.”, it says “content will inevitably differ”.