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
To generate a _stable_ UID/Key for a given `item`, consistently between client and server, __in 900 bytes__.
10
+
To generate a _stable_ UID/Key for a given `item`, consistently between client and server, **in 900 bytes**.
11
+
12
+
⚠️ SSR: **Not compatible with Strict or Concurent mode**. Consider using _native_`useId`(React 18) hook instead.
11
13
12
-
⚠️ __Not compatible with Strict or Concurent mode__. Consider using _native_`useId`(React 18) hook instead.
14
+
> If your clientside is using StrictMode it will never match SSR-ed Ids due to double invocation
13
15
14
16
Example - https://codesandbox.io/s/kkmwr6vv47
15
17
16
18
## API
19
+
17
20
React UID provides 3 different APIs
18
-
- vanilla js API - `uid(item) -> key`
21
+
22
+
- vanilla js API - `uid(item) -> key`
19
23
- React Component, via renderProp based API - `<UID>{ id => <><label htmlFor={id}/><input id={id}/></>}</UID>`
20
24
- React Hooks - `useUID`
21
25
22
26
#### Javascript
27
+
23
28
-`uid(item, [index])` - generates UID for an object(array, function and so on), result could be used as React `key`.
24
-
`item` should be an object, but could be anything. In case it is not an "object", and might have non-unique value - you have to specify second argument - `index`
29
+
`item` should be an object, but could be anything. In case it is not an "object", and might have non-unique value - you have to specify second argument - `index`
The difference between `uid` and `UID` versions are in "nesting" - any `UID` used inside another `UID` would contain "parent prefix" in the result, scoping `uid` to the local tree branch.
83
92
84
-
UID might be NOT __SSR friendly__,
93
+
UID might be NOT **SSR friendly**,
85
94
86
95
#### Hooks (16.8+)
96
+
87
97
-`useUID()` will generate a "stable" UID
88
98
-`useUIDSeed()` will generate a seed generator, you can use for multiple fields
99
+
89
100
```js
90
101
import { useUID, useUIDSeed } from'react-uid';
91
102
92
103
constForm= () => {
93
-
constuid=useUID();
104
+
constuid=useUID();
94
105
return (
95
106
<>
96
107
<label htmlFor={uid}>Email:</label>
@@ -100,7 +111,7 @@ const Form = () => {
100
111
}
101
112
102
113
constForm= () => {
103
-
constseed=useUIDSeed();
114
+
constseed=useUIDSeed();
104
115
return (
105
116
<>
106
117
<label htmlFor={seed('email')}>Email:</label>
@@ -110,44 +121,48 @@ const Form = () => {
110
121
)
111
122
}
112
123
```
113
-
Hooks API __is SSR friendly__,
124
+
125
+
Hooks API **is SSR friendly**,
114
126
115
127
### Server-side friendly UID
116
128
117
129
- `UIDReset`, `UIDConsumer`, `UIDFork` - SSR friendly UID. Could maintain consistency across renders.
118
-
They are much more complex than `UID`, and provide functionality you might not need.
130
+
They are much more complex than `UID`, and provide functionality you might not need.
119
131
120
-
The key difference - they are not using global "singlentone" to track used IDs,
132
+
The key difference - they are not using global "singlentone" to track used IDs,
121
133
but read it from Context API, thus works without side effects.
122
134
123
135
Next example will generate the same code, regardless how many time you will render it
124
-
```js
125
-
import {UIDReset, UIDConsumer} from'react-uid';
126
136
127
-
<UIDReset>
128
-
<UIDConsumer>
129
-
{(id,uid) => (
130
-
<Fragment>
131
-
<input id={id} />
132
-
<label htmlFor={id} />
133
-
data.map( item=><li key={uid(item)}>{item}</li>)
134
-
</Fragment>
135
-
)}
136
-
</UIDConsumer>
137
-
</UIDReset>
137
+
```js
138
+
import { UIDReset, UIDConsumer } from'react-uid';
139
+
140
+
<UIDReset>
141
+
<UIDConsumer>
142
+
{(id, uid) => (
143
+
<Fragment>
144
+
<input id={id} />
145
+
<label htmlFor={id} />
146
+
data.map( item=><li key={uid(item)}>{item}</li>)
147
+
</Fragment>
148
+
)}
149
+
</UIDConsumer>
150
+
</UIDReset>;
138
151
```
139
152
140
-
__UID__ is not 100% SSR friendly - use __UIDConsumer__.
153
+
**UID** is not 100% SSR friendly - use **UIDConsumer**.
141
154
142
155
### Code splitting
156
+
143
157
Codesplitting may affect the order or existence of the components, so alter
144
158
the `componentDidMount` order, and change the generated ID as result.
145
159
146
160
In case of SPA, this is not something you should be bothered about, but for SSR
147
161
this could be fatal.
148
162
149
-
Next example will generate consistent keys regardless of component mount order.
150
-
Each call to `UIDFork` creates a new branch of UIDs untangled from siblings.
163
+
Next example will generate consistent keys regardless of component mount order.
164
+
Each call to `UIDFork` creates a new branch of UIDs untangled from siblings.
0 commit comments