React useId Simplified

Pro Members Only

This lesson is available if you have an active subscription.

Alternatively, some member might be able to gift it to you.

If you already have a subscription, you can sign in.

React useId Simplified

Subscription Required

You must have an active subscription to access this content.
If you already have a subscription, you can sign in.

Use case for React useId

When working with HTML forms inputs and labels are linked together with input.id and label.htmlFor. However, in browsers there can be only one input with a given id at a point in time.

This means that if we have two form, with fields containing the same id, it results in labels being attached to the first input that matched the id. This is demonstrated in the example below where we have two forms, but the labels in the second form attach to the inputs of the first form.

App.tsx
PersonForm.tsx
import { PersonForm } from "./PersonForm";

export default function App() {
return (
<>
<h1>Host</h1>
<PersonForm />
<h1>Guest</h1>
<PersonForm />
</>
);
}

Unique id identifiers with React useId hook

We can utilize the useId hook in the forms to ensure that react creates a unique id for each form. This ensures that the labels in the form attach to only the inputs in the form.

This is demonstrated below where we have two instances of the form, and the labels attach to the correct inputs.

PersonForm.tsx
App.tsx
import { useId } from "react";

export function PersonForm() {
const id = useId();
return (
<div>
<label htmlFor={id + "-firstName"}>First Name</label>
<div>
<input id={id + "-firstName"} type="text" />
</div>
<label htmlFor={id + "-lastName"}>Last Name</label>
<div>
<input id={id + "-lastName"} type="text" />
</div>
</div>
);
}

Why not create your own random ids?

Note that these id are also stable and preserved in all renderings of the component e.g.

  • Server side rendering => Client side rehydration
  • Client side re-rendering of the component

So React.useId is better than some code you might right to create random ids.

javascript
typescript
react
playwright

Enjoy free content straight from your inbox 💌

No spam, unsubscribe at any time.

Transcript

00:00

Use ID is perhaps one of the less celebrated features of React, but if you are working with Form Fields, it is a critically useful one, and that is exactly what we will cover in this tutorial. So let's go. The use ID hook finds its main use case within forms, which is what 90% of software development is for some software developers To demonstrate ity, we have a simple person form that has two fields, first name and last name. Each field has the label and an input, and we use the attribute HTML four to link the label to the input with a given id. Now this label to input with HTML four

00:34

and ID linking provides a number of accessibility features. So to demonstrate some of that, let's put this form into our app component. We put a simple heading to specify that we are collecting the details for a host and then use our person form. And this is what our app component looks like when rendered within the browser. A key feature provided by that attribute linking is that we can click the labels and it'll automatically focus the user onto the specified input. But there is one key issue with our person form implementation. If we have two instances of the person form, for example, one for the host and one for the guest.

01:07

When we view the application in the browser, clicking on the first name and the last name labels for the guest takes us to the input fields on the host. And we are also violating an HDML rule that a particular value of the ID attribute should only appear on one dome element in the entire document. This is because JavaScript creates a variable for each element with an id. And if you have two elements with the same id, then that variable is going to become ambiguous. And these are the issues that the use ID hook solves by creating stable, unique identifiers for every component that uses the use Id hook to fix our code,

01:42

we bring in the use ID hook, which is available from the main React package, and then invoke this hook at the root level of our person. Form. This ID will be unique for our entire document, and we can use this as a prefix to create unique IDs for our individual fields. Now with this change in place, clicking on the first name and the last name labels for the host takes us to the host inputs, and clicking on the labels for the guest takes us to the guest inputs. In terms of the internals, you should treat the values generated by use ID as opaque. But if you are curious like me, these are the values

02:14

that got generated during this test. The use ID hook is just one of the many ways in which React makes our lives as web developers much easier. So whenever you're thinking of using forms, think about using the use Id hook. Thank you for joining me, and I will see you in the next one.