patternreactCritical
React 18: Hydration failed because the initial UI does not match what was rendered on the server
Viewed 0 times
failedwhathydrationmatchthewasdoesreactnotserver
Problem
I'm trying to get SSR working in my app, but I get the error:
Hydration failed because the initial UI does not match what was
rendered on the server.
Live demo code is here.
Live demo of problem is here (open dev tools console to see the errors):
File App.js
File index.js
The HTML template shown in the live demo is served by the backend and generated using the following code:
I need to dynamically generate
Hydration failed because the initial UI does not match what was
rendered on the server.
Live demo code is here.
Live demo of problem is here (open dev tools console to see the errors):
File App.js
import React from "react";
class App extends React.Component {
head() {
return (
React App
);
}
body() {
return (
Client says Hello World
);
}
render() {
return (
{this.head()}
{this.body()}
)
}
}
export default App;File index.js
import React from "react";
import * as ReactDOM from "react-dom/client";
import { StrictMode } from "react";
import App from "./App";
//const container = document.getElementById("root");
const container = document.getElementsByTagName("html")[0]
ReactDOM.hydrateRoot(
container,
);The HTML template shown in the live demo is served by the backend and generated using the following code:
const ReactDOMServer = require('react-dom/server');
const clientHtml = ReactDOMServer.renderToString(
)
// Serve clientHtml to clientI need to dynamically generate
and section as shown in the App class.Solution
I have been experiencing the same problem lately with Next.js.
I am not sure if my observations apply to other libraries.
I had been wrapping my components with an improper tag; that is, Next.js is not comfortable having a p tag wrapping divs, sections, etc., so it will yell Hydration failed because the initial UI does not match what was rendered on the server.
I solved this problem by examining how my elements were wrapping each other. With Material UI, you would need to be cautious. For example, if you use a typography component as a wrapper, the default value of the component prop is
Based on my personal experience, the problem is caused by the improper arrangement of HTML elements. To solve the problem in the context of Next.js, one needs to reevaluate how HTML elements are arranged.
I am not sure if my observations apply to other libraries.
I had been wrapping my components with an improper tag; that is, Next.js is not comfortable having a p tag wrapping divs, sections, etc., so it will yell Hydration failed because the initial UI does not match what was rendered on the server.
I solved this problem by examining how my elements were wrapping each other. With Material UI, you would need to be cautious. For example, if you use a typography component as a wrapper, the default value of the component prop is
, so you will experience the error if you don’t change the component value to something semantic.Based on my personal experience, the problem is caused by the improper arrangement of HTML elements. To solve the problem in the context of Next.js, one needs to reevaluate how HTML elements are arranged.
import Image from 'next/image'
/**
* This might give that error
*/
export const IncorrectComponent = ()=>{
return(
This is not correct and should never be done
because the p tag has been abused
)
}
/**
* This will work
*/
export const CorrectComponent = ()=>{
return(
This is correct and should work because a div is
really good for this task.
)
}
Context
Stack Overflow Q#71706064, score: 450
Revisions (0)
No revisions yet.