Skip to content

QuizNot attempted

useId across instances

Do the two <Field /> instances render <input> elements with the same id value or different ones?
function Field() {
const id = useId();
return (
<div>
<label htmlFor={id}>Name</label>
<input id={id} />
</div>
);
}
function App() {
return (
<>
<Field />
<Field />
</>
);
}
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • Different, unique id values for each instance
  • useId generates an id that is unique per component instance (and stable between server and client), so the two Fields get different ids.
  • Not 'same id': avoiding collisions is the whole point.
  • Not 'undefined': it always returns a string.
  • Not 'throws': calling the same hook in two instances is normal.