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

[Bug]: useId triggers a hydration mismatch error #369

Closed
Valerioageno opened this issue Jan 18, 2025 · 2 comments · Fixed by #371
Closed

[Bug]: useId triggers a hydration mismatch error #369

Valerioageno opened this issue Jan 18, 2025 · 2 comments · Fixed by #371
Assignees
Labels
bug Something isn't working typescript Requires typescript knowledge

Comments

@Valerioageno
Copy link
Member

Valerioageno commented Jan 18, 2025

Description

Using useId always trigger a hydration mismatch between server and client

Expected behaviour

No hydration errors should be triggered

How to reproduce

  1. Create any new tuono project tuono new project
  2. Add a useId and render it in any place of the application
// index.tsx
import type { JSX } from "react";
import { useId } from "react";


export default function IndexPage(): JSX.Element {
  const myId = useId();

  return <>{myId}</>
}

Screenshots

Image

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish)
    CPU: (20) x64 13th Gen Intel(R) Core(TM) i5-13600KF
    Memory: 11.69 GB / 15.53 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 20.9.0 - ~/.nvm/versions/node/v20.9.0/bin/node
    Yarn: 1.22.21 - ~/.nvm/versions/node/v20.9.0/bin/yarn
    npm: 10.1.0 - ~/.nvm/versions/node/v20.9.0/bin/npm
    pnpm: 9.12.3 - ~/.local/share/pnpm/pnpm
  npmPackages:
    tuono: 0.17.1 => 0.17.1

System info (Rust)

rustc 1.82.0 (f6e511eec 2024-10-15)
cargo 1.82.0 (8f40fc59f 2024-08-21)
tuono 0.17.1

Additional context

#364 (comment)

@Valerioageno Valerioageno added bug Something isn't working typescript Requires typescript knowledge labels Jan 18, 2025
@Valerioageno Valerioageno moved this to Ready in Road to V1 Jan 18, 2025
@marcalexiei
Copy link
Member

From #364 (comment):

I'm wondering if the problem can be the differences between the script/meta tags in the server/client.

// server
const stream = await renderToReadableStream(
      <>
        <RouterProvider router={router} serverProps={serverProps as never} />

        {mode === 'Dev' && <DevResources />}
        {mode === 'Prod' && (
          <ProdResources cssBundles={cssBundles} jsBundles={jsBundles} />
        )}

        <script>{`window.__TUONO_SSR_PROPS__=${payload as string}`}</script>
      </>,
    )
// client
 hydrateRoot(
    document,
    <React.StrictMode>
      <RouterProvider router={router} />
    </React.StrictMode>,
  )

@marcalexiei
Copy link
Member

Few useful reference from the official documentation: https://react.dev/reference/react/useId#usage:

Pitfall:
With server rendering, useId requires an identical component tree on the server and the client. If the trees you render on the server and the client don’t match exactly, the generated IDs won’t match.


Why is useId better than an incrementing counter?
[...]
Inside React, useId is generated from the “parent path” of the calling component. This is why, if the client and the server tree are the same, the “parent path” will match up regardless of rendering order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working typescript Requires typescript knowledge
Projects
Status: Done
2 participants