useId
Returns an id that is identical on server and client for the same position in the owner tree.
Returns an id that is identical on server and client for the same position in the owner tree.
The id uses only letters, digits, - and _, so it is valid in id, for, aria-* references
and in querySelector("#" + id) without escaping. Use it to link a label to its input, or an
element to its aria-describedby target, in markup that is rendered on the server and hydrated.
Import#
ts
import { useId } from "@shadow-js/core";
Signature#
ts
function useId(): string
Returns#
A new id, unique within the root; each call on the same owner returns the next one.
Throws#
- ShadowError
NO_OWNER_IDwhen called outside any owner, because no stable id exists there.
Examples#
ts
import { useId } from "@shadow-js/core";
function EmailField() {
const id = useId();
return (
<>
<label for={id}>Email</label>
<input id={id} type="email" />
</>
);
}