You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/routes/reference/component-apis/create-unique-id.mdx
+42-7Lines changed: 42 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,23 +9,58 @@ tags:
9
9
- ssr
10
10
- forms
11
11
- utilities
12
-
version: '1.0'
12
+
version: "1.0"
13
13
description: >-
14
14
Generate unique IDs that are stable across server and client with
15
15
createUniqueId. Essential for accessible forms and SSR-compatible components.
16
16
---
17
17
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.
20
28
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
+
:::
22
31
32
+
## Import
33
+
34
+
```ts
35
+
import { createUniqueId } from"solid-js";
23
36
```
24
37
25
-
A universal id generator that is stable across server/browser.
38
+
## Type
26
39
27
40
```ts
28
-
const id =createUniqueId()
41
+
functioncreateUniqueId():string;
29
42
```
30
43
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.
0 commit comments