Skip to content

React.useId() usage results in hydration mismatch #43033

Closed
@rijk

Description

@rijk

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

next info:
    Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.1.0: Sun Oct  9 20:15:52 PDT 2022; root:xnu-8792.41.9~2/RELEASE_ARM64_T8112
    Binaries:
      Node: 16.18.1
      npm: 8.19.2
      Yarn: N/A
      pnpm: N/A
    Relevant packages:
      next: 13.0.4-canary.3
      eslint-config-next: 13.0.2
      react: 18.2.0
      react-dom: 18.2.0

What browser are you using? (if relevant)

Chrome 107.0.5304.110 (Official Build) (arm64)

How are you deploying your application? (if relevant)

Vercel (not relevant though)

Describe the Bug

Related to #30876, perhaps a regression.

When using React.useId() from a client component, React throws a hydration error:

'use client'

import { useId } from 'react'

export default function IdCheck() {
  const id = useId()
  return <div id={id}>ID: {id}</div>
}
Error: Hydration failed because the initial UI does not match what was rendered on the server.

See more info here: https://nextjs.org/docs/messages/react-hydration-error

This causes issues in libraries like Headless UI: tailwindlabs/headlessui#1961

Expected Behavior

No error.

Link to reproduction - Issues with a link to complete (but minimal) reproduction code will be addressed faster

https://codesandbox.io/s/cool-shannon-duxx1w?file=/app/page.tsx

To Reproduce

Note that I couldn't get the Sandbox to run because the app directory requires node 16 and Codesandbox uses node 14.

Metadata

Metadata

Assignees

No one assigned

    Labels

    UpstreamRelated to using Next.js with a third-party dependency. (e.g., React, UI/icon libraries, etc.).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions