next.js icon indicating copy to clipboard operation
next.js copied to clipboard

another possible fix for hydration error

Open abdennor opened this issue 3 years ago • 3 comments

I had the same issue, so the fix that worked for me was pulled from this thread https://stackoverflow.com/a/71870995

I have been experiencing the same problem lately with NextJS 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, NextJS 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". So 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. So 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 NextJS one will have to reevaluate how they are arranging their html element

Bug

  • [x] Related issues linked using fixes #number
  • [ ] Integration tests added
  • [x] Errors have a helpful link attached, see contributing.md

Feature

  • [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
  • [ ] Related issues linked using fixes #number
  • [ ] Integration tests added
  • [ ] Documentation added
  • [ ] Telemetry added. In case of a feature if it's used or not.
  • [x] Errors have a helpful link attached, see contributing.md

Documentation / Examples

  • [x] Make sure the linting passes by running pnpm lint
  • [ ] The "examples guidelines" are followed from our contributing doc

abdennor avatar Sep 19 '22 10:09 abdennor

Rather than this specific example, of div inside p, I would suggest that this needs to be worded something like Invalid HTML may cause a hydration mismatch I don't think having the stackoverflow link is a great idea as the answer isn't correct enough.

awareness481 avatar Sep 19 '22 18:09 awareness481

@awareness481 yes, that would be more appropriate thank you

abdennor avatar Sep 20 '22 08:09 abdennor

Agree , I can't put div inside p

thanhtutzaw avatar Sep 21 '22 07:09 thanhtutzaw