Generating stable unique ids
A label points at its input through id. Render the same field twice and the two
labels need two different ids. Hard-coding one works until the second copy
appears. Generating a random id in render does not work either — it changes on
every render.
useId gives each component instance a unique id that stays put:
import { useId } from 'react';
function Field({ label }) {
const id = useId();
return (
<div>
<label htmlFor={id}>{label}</label>
<input id={id} />
</div>
);
}
Two <Field label="Email" /> render with different ids, so each label targets
its own input. The id is the same across re-renders and — important for server
rendering — the same on the server and the client, so hydration does not
mismatch.
Do not use useId as a key. Keys come from your data, not from the hook.
Your job is to give each rendered field a unique id with useId.
Challenge
Two fields share the same id, so each label points at the wrong input. Export an
App component from App.jsx that renders two Field components, each
labelled Email. Field takes a label prop and wires a <label htmlFor> to an
<input id> with useId, so each instance gets its own id.
The Test renders both fields and expects their ids to differ. The starter uses a fixed id, so the Test fails.