Skip to content

Commit d84c1c1

Browse files
committed
Update createUniqueId reference
1 parent 263cedd commit d84c1c1

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

src/routes/reference/component-apis/create-unique-id.mdx

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,58 @@ tags:
99
- ssr
1010
- forms
1111
- utilities
12-
version: '1.0'
12+
version: "1.0"
1313
description: >-
1414
Generate unique IDs that are stable across server and client with
1515
createUniqueId. Essential for accessible forms and SSR-compatible components.
1616
---
1717

18-
```ts
19-
import { createUniqueId } from "solid-js"
18+
The `createUniqueId` function generates a unique ID that remains consistent across both server and client renders.
19+
20+
This function is commonly used for HTML `id` and `for` attributes to ensure stable hydration.
21+
22+
It is important to note that `createUniqueId` does not generate a cryptographically secure ID and is not suitable for security-sensitive data.
23+
Additionally, it is not appropriate for scenarios that require uniqueness across a distributed system.
24+
25+
:::note
26+
`createUniqueId` relies on a counter-based mechanism to generate IDs.
27+
It must be called the same number of times on both the server and client.
2028

21-
function createUniqueId(): string
29+
Calling `createUniqueId` only on the server or only on the client—such as when using [`isServer`](/reference/rendering/is-server) or [`<NoHydration>`](/reference/components/no-hydration)—may lead to hydration errors.
30+
:::
2231

32+
## Import
33+
34+
```ts
35+
import { createUniqueId } from "solid-js";
2336
```
2437

25-
A universal id generator that is stable across server/browser.
38+
## Type
2639

2740
```ts
28-
const id = createUniqueId()
41+
function createUniqueId(): string;
2942
```
3043

31-
**Note:** on the server this only works under hydratable components.
44+
## Parameters
45+
46+
This function does not take any parameters.
47+
48+
## Returns
49+
50+
`createUniqueId` returns a unique `string` that is stable across server and client renders.
51+
52+
## Examples
53+
54+
### Basic Usage
55+
56+
```tsx
57+
import { createUniqueId } from "solid-js";
58+
59+
type InputProps = {
60+
id?: string;
61+
};
62+
63+
function Input(props: InputProps) {
64+
return <input id={props.id ?? createUniqueId()} />;
65+
}
66+
```

0 commit comments

Comments
 (0)