useId generates unique, consistent IDs for SSR compatibility.
Code:
1import { useId } from "react";23function FormField({ label, error }) {4 const inputId = useId();5 const errorId = useId();67 return (8 <div>9 <label htmlFor={inputId}>{label}</label>10 <input11 id={inputId}12 aria-describedby={error ? errorId : undefined}13 aria-invalid={!!error}14 />15 {error && <span id={errorId} className="error">{error}</span>}16 </div>17 );18}
Why not Math.random():
Why not index in list:
Generated IDs:
:r1:, :r2:, etc.Use cases: