Skip to content

Commit bee9b70

Browse files
committed
chore: refresh code
1 parent 70b3734 commit bee9b70

13 files changed

+4385
-7194
lines changed

.husky/pre-commit

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
lint-staged

README.md

Lines changed: 69 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,56 @@
1-
UID
2-
=======
1+
# UID
2+
33
[![Build Status](https://travis-ci.org/thearnica/react-uid.svg?branch=master)](https://travis-ci.org/thearnica/react-uid)
44
[![coverage-badge](https://img.shields.io/codecov/c/github/thearnica/react-uid.svg?style=flat-square)](https://codecov.io/github/thearnica/react-uid)
55
[![NPM version](https://img.shields.io/npm/v/react-uid.svg)](https://www.npmjs.com/package/react-uid)
66
[![Greenkeeper badge](https://badges.greenkeeper.io/thearnica/react-uid.svg)](https://greenkeeper.io/)
77
[![bundle size](https://badgen.net/bundlephobia/minzip/react-uid)](https://bundlephobia.com/result?p=react-uid)
88
[![downloads](https://badgen.net/npm/dm/react-uid)](https://www.npmtrends.com/react-uid)
99

10-
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.
1113

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
1315
1416
Example - https://codesandbox.io/s/kkmwr6vv47
1517

1618
## API
19+
1720
React UID provides 3 different APIs
18-
- vanilla js API - `uid(item) -> key`
21+
22+
- vanilla js API - `uid(item) -> key`
1923
- React Component, via renderProp based API - `<UID>{ id => <><label htmlFor={id}/><input id={id}/></>}</UID>`
2024
- React Hooks - `useUID`
2125

2226
#### Javascript
27+
2328
- `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`
30+
2531
```js
26-
import {uid} from 'react-uid';
27-
28-
// objects
29-
const data = [{a:1}, {b:2}];
30-
data.map( item => <li key={uid(item)}>{item}</li>)
31-
32-
// unique strings
33-
const data = ["a", "b"];
34-
data.map( item => <li key={uid(item)}>{item}</li>)
35-
36-
// strings
37-
const data = ["a", "a"];
38-
data.map( (item, index) => <li key={uid(item, index)}>{item}</li>)
39-
```
40-
41-
JS API might be NOT (multi-tenant)__SSR friendly__,
32+
import { uid } from 'react-uid';
33+
34+
// objects
35+
const data = [{ a: 1 }, { b: 2 }];
36+
data.map((item) => <li key={uid(item)}>{item}</li>);
37+
38+
// unique strings
39+
const data = ['a', 'b'];
40+
data.map((item) => <li key={uid(item)}>{item}</li>);
41+
42+
// strings
43+
const data = ['a', 'a'];
44+
data.map((item, index) => <li key={uid(item, index)}>{item}</li>);
45+
```
46+
47+
JS API might be NOT (multi-tenant)**SSR friendly**,
4248

4349
#### React Components
50+
4451
- (deprecated)`UID` - renderless container for generation Ids
4552
- `UIDConsumer` - renderless container for generation Ids
53+
4654
```js
4755
import {UID} from 'react-uid';
4856

@@ -51,7 +59,7 @@ JS API might be NOT (multi-tenant)__SSR friendly__,
5159
<Fragment>
5260
<input id={id} />
5361
<label htmlFor={id} />
54-
</Fragment>
62+
</Fragment>
5563
)}
5664
</UID>
5765

@@ -64,33 +72,36 @@ JS API might be NOT (multi-tenant)__SSR friendly__,
6472
</Fragment>
6573
)}
6674
</UID>
67-
75+
6876
// UID also provide `uid` as a second argument
6977
<UID>
7078
{(_, uid) => (
71-
data.map( item => <li key={uid(item)}>{item}</li>)
79+
data.map( item => <li key={uid(item)}>{item}</li>)
7280
)}
7381
</UID>
74-
82+
7583
// in the case `item` is not an object, but number or string - provide and index
7684
<UID>
7785
{(_, uid) => (
78-
data.map( (item, index) => <li key={uid(item, index)}>{item}</li>)
86+
data.map( (item, index) => <li key={uid(item, index)}>{item}</li>)
7987
)}
8088
</UID>
8189
```
90+
8291
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.
8392

84-
UID might be NOT __SSR friendly__,
93+
UID might be NOT **SSR friendly**,
8594

8695
#### Hooks (16.8+)
96+
8797
- `useUID()` will generate a "stable" UID
8898
- `useUIDSeed()` will generate a seed generator, you can use for multiple fields
99+
89100
```js
90101
import { useUID, useUIDSeed } from 'react-uid';
91102

92103
const Form = () => {
93-
const uid = useUID();
104+
const uid = useUID();
94105
return (
95106
<>
96107
<label htmlFor={uid}>Email: </label>
@@ -100,7 +111,7 @@ const Form = () => {
100111
}
101112

102113
const Form = () => {
103-
const seed = useUIDSeed();
114+
const seed = useUIDSeed();
104115
return (
105116
<>
106117
<label htmlFor={seed('email')}>Email: </label>
@@ -110,44 +121,48 @@ const Form = () => {
110121
)
111122
}
112123
```
113-
Hooks API __is SSR friendly__,
124+
125+
Hooks API **is SSR friendly**,
114126
115127
### Server-side friendly UID
116128
117129
- `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.
119131
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,
121133
but read it from Context API, thus works without side effects.
122134
123135
Next example will generate the same code, regardless how many time you will render it
124-
```js
125-
import {UIDReset, UIDConsumer} from 'react-uid';
126136
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>;
138151
```
139152
140-
__UID__ is not 100% SSR friendly - use __UIDConsumer__.
153+
**UID** is not 100% SSR friendly - use **UIDConsumer**.
141154
142155
### Code splitting
156+
143157
Codesplitting may affect the order or existence of the components, so alter
144158
the `componentDidMount` order, and change the generated ID as result.
145159
146160
In case of SPA, this is not something you should be bothered about, but for SSR
147161
this could be fatal.
148162
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.
165+
151166
```js
152167
import {UIDReset, UIDFork, UIDConsumer} from 'react-uid';
153168

@@ -163,18 +178,20 @@ import {UIDReset, UIDFork, UIDConsumer} from 'react-uid';
163178
<UIDConsumer>
164179
{ uid => <span>{uid} is unique </span>}
165180
</UIDConsumer>
166-
</UIDFork>
181+
</UIDFork>
167182
</UIDReset>
168183
```
169184
170185
The hooks API only needs the `<UIDFork>` wrapper.
171186
172187
### So hard?
188+
173189
"Basic API" is not using Context API to keep realization simple, and React tree more flat.
174190
175191
# Types
192+
176193
Written in TypeScript
177194
178195
# Licence
179-
MIT
180-
196+
197+
MIT
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`uid hooks in strict mode test uid 1`] = `"<div><i>1 == 2 ?</i></div>"`;

0 commit comments

Comments
 (0)