Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readReact

Generate hydration-safe IDs with useId

Use useId to associate labels and inputs with matching IDs on the server and during hydration.

You give each input an id so its label can point at it, and you build the id with Math.random(). The page works in the browser. Once the page is server rendered, React logs a hydration warning, because the server and the browser picked different ids.

function Field({ label }: { label: string }) {
  const id = 'field-' + Math.random().toString(36).slice(2, 7)
  return (
    <div>
      <label htmlFor={id}>{label}</label>
      <input id={id} />
    </div>
  )
}
// renderToString, run twice:
// <label for="field-38ezg">Email</label><input id="field-38ezg"/>
// <label for="field-u3d76">Email</label><input id="field-u3d76"/>

The server renders the component once and the browser renders it again to hydrate the HTML, so one input gets two ids. To see it, call renderToString from react-dom/server twice on <Field label="Email" /> and compare the strings.

I also put the server string into a jsdom page and called hydrateRoot from react-dom/client on it. React logged: "A tree hydrated but some attributes of the server rendered HTML didn't match the client properties."

A module-level counter depends on the order of renders. Consecutive server requests can advance it, and the browser may hydrate components in a different order.

Let React hand out the id

useId returns a distinct ID for each call within a root. With the same component tree on the server and client, React generates matching IDs during hydration. Call it at the top of the component, like any other hook.

import { useId } from 'react'

function Field({ label }: { label: string }) {
  const id = useId()
  return (
    <div>
      <label htmlFor={id}>{label}</label>
      <input id={id} />
    </div>
  )
}
// renderToString, run twice, form with two Fields:
// first run: one input with id A, one with id B
// second run: the same, A and B

Both renders printed the same two ids, and the two Field instances inside one render got different ones. The same jsdom hydration run with useId logged nothing.

Treat the string as opaque. Its format has changed between React releases, so do not parse it, slice it or write a selector for it.

One call, several inputs

A component with several inputs needs one useId call. Add a suffix for each input.

function SignupFields() {
  const id = useId()
  return (
    <fieldset>
      <label htmlFor={`${id}-email`}>Email</label>
      <input id={`${id}-email`} />
      <label htmlFor={`${id}-name`}>Name</label>
      <input id={`${id}-name`} />
    </fieldset>
  )
}
// two copies: A-email, A-name, then B-email, B-name

Each copy of SignupFields has its own prefix, so each label points at the input in that copy. For multiple independently hydrated roots, give each root a different identifierPrefix and pass that same prefix to its server renderer and hydrateRoot.