I need to dynamically generate <head></head> and <body></body>
section as shown in the App class.
I have been experiencing the same problem lately with Next.js and I am not sure if my observations are applicable 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 your 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 "p", so you will experience the error if you don't change the component value to something semantic.
In my own opinion, based on my personal experience, the problem is caused by improper arrangement of HTML elements and to solve the problem in the context of Next.js, one will have to reevaluate how they are arranging their HTML element.
import Image from 'next/image'
/**
* This might give that error
*/
export const IncorrectComponent = ()=>{
return(
<p>
<div>This is not correct and should never be done because the p tag has been abused</div>
<Image src='/vercel.svg' alt='' width='30' height='30'/>
</p>
)
}
/**
* This will work
*/
export const CorrectComponent = ()=>{
return(
<div>
<div>This is correct and should work because a div is really good for this task.</div>
<Image src='/vercel.svg' alt='' width='30' height='30'/>
</div>
)
}