Skip to content

Unique IDs

uniqueId

Allocate an ID that is unique in the current thread.

import { uniqueId } from "rvx/id";

const id = uniqueId();

<>
    <label for={id}>Name</label>
    <input type="text" id={id} />
</>;
import { uniqueId, e } from "./rvx.js";

const id = uniqueId();

[
    e("label").set("for", id).append("Name"),
    e("input").set("type", "text").set("id", id),
]
  • Returns a string in the form "rvx_{suffix}". E.g. "rvx_77"
  • There are practically infinite ids, however when the Number.MAX_SAFE_INTEGER suffix is reached, the current implementation switches to using BigInts which will slightly degrade allocation performance.

<UseUniqueId>

A component for allocating a unique id using uniqueId.

import { UseUniqueId } from "rvx/id";

<UseUniqueId>
    {id => <>
        <label for={id}>Name</label>
        <input type="text" id={id} />
    </>}
</UseUniqueId>
import { UseUniqueId, e } from "./rvx.js";

UseUniqueId({
    children: id => [
        e("label").set("for", id).append("Name"),
        e("input").set("type", "text").set("id", id),
    ],
})

uniqueIdFor

Get a unique id for the specified object.

import { uniqueIdFor, For } from "rvx/id";

<ul>
    <For each={items}>
        {item => <li id={uniqueIdFor(item)}>...</li>}
    </For>
</ul>
import { uniqueIdFor, forEach, e } from "./rvx.js";

e("ul").append(
    forEach(items, item => e("li").set("id", uniqueIdFor(item)).append(...))
)