[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hydration error in next.js when using the div tag instead of the main tag #61275

Open
nishant-k1 opened this issue Jan 28, 2024 · 4 comments
Open
Labels
bug Issue was opened via the bug report template.

Comments

@nishant-k1
Copy link
nishant-k1 commented Jan 28, 2024

Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/charming-chihiro-vyhcff?file=%2Fapp%2Fpage.tsx%3A4%2C2

To Reproduce

The error that I get

image

To reproduce the issue below is the code that caused the error

import React from "react";
import Image from "next/image";

type Props = {};

const Testing = (props: Props) => {
  return (
    <div>
      <h1>Testing</h1>
    </div>
  );
};

export default Testing;

Below is the code that runs flawlessly

import React from "react";
import Image from "next/image";

type Props = {};

const Testing = (props: Props) => {
  return (
    <main>
      <h1>Testing</h1>
    </main>
  );
};

export default Testing;

Current vs. Expected behavior

You can see the only difference is the usage of the div tag instead of the main tag causing the hydration error.

The error can be reproduced only locally while using vs code. Weirdly, I'm unable to reproduce the error on CodeSandbox.
While going through the documentation I don't see an explanation related to the usage of semantic tags or anything related. It only mentions incorrect nesting of tags. Maybe I'm missing something?

Provide environment information

npm info using npm@9.6.4
npm info using node@v20.1.0
npm info using npm@9.6.4
npm info using node@v20.1.0
npm info ok 
npm info using npm@9.6.4
npm info using node@v20.1.0
npm info ok 

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 21.6.0: Thu Nov  9 00:42:05 PST 2023; root:xnu-8020.240.18.705.10~1/RELEASE_ARM64_T6000
Binaries:
  Node: 20.1.0
  npm: 9.6.4
  Yarn: 4.0.2
  pnpm: 8.8.0
Relevant Packages:
  next: 14.1.1-canary.17 // Latest available version is detected (14.1.1-canary.17).
  eslint-config-next: 14.1.0
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.3.3
Next.js Config:
  output: N/A


npm info ok

Which area(s) are affected? (Select all that apply)

Not sure

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

No response

@nishant-k1 nishant-k1 added the bug Issue was opened via the bug report template. label Jan 28, 2024
@theStrangeAdventurer
Copy link
theStrangeAdventurer commented Jan 31, 2024

I have same error, but with button in button (NextJS v13)

Minimal case is

const Page: NextPage = () => {
    return (
        <>
            <button ><button>test</button></button>
        </>
    );
};
Снимок экрана 2024-01-31 в 15 19 00

I tried to add different keys, and use div between buttons like that

            <button><div><button>test2</button></div></button>

UPDATE

It's a strange (markup is same on client and server), but it turns out that my case is described in the documentation. https://nextjs.org/docs/messages/react-hydration-error#common-causes

@davelin18yufan
Copy link

Hey, this doesn't seem to be a significant issue. I forked your link, and initially, I encountered this error. However, after saving the file and letting the program restart once, everything worked fine. I guess it might be a version issue. The version of next in the yarn.lock file is 13.0.8-canary.0.

@nishant-k1
Copy link
Author
nishant-k1 commented Feb 6, 2024

Hey, this doesn't seem to be a significant issue. I forked your link, and initially, I encountered this error. However, after saving the file and letting the program restart once, everything worked fine. I guess it might be a version issue. The version of next in the yarn.lock file is 13.0.8-canary.0.

Thanks @davelin18yufan and I appreciate your effort for taking a closer look at the issue. I was kind of forced to use 13.0.8-canary.0 to create the issue here on GitHub. (It's the protocol for raising the issue here) The error occurs on both the normal version and the canary version so it's not the version issue. Unfortunately, this doesn't seem significant but it took a lot of effort for me to debug and find the reason for the hydration issue in one of my big Projects. I was assuming the reason behind the hydration issue might be something else but I was quite surprised to find this reason which seems weird.

@tj-dixon
Copy link

This is a frequently occurring issue while using MUI components.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

4 participants