Skip to content

Commit 9a0d485

Browse files
authored
Merge pull request #31 from thearnica/update-may-2020
Update may 2020
2 parents 4228732 + 053f8b6 commit 9a0d485

File tree

9 files changed

+2825
-1261
lines changed

9 files changed

+2825
-1261
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ UID
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 UID/Key for an `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__.
1111

1212
Example - https://codesandbox.io/s/kkmwr6vv47
1313

@@ -36,10 +36,11 @@ React UID provides 3 different APIs
3636
data.map( (item, index) => <li key={uid(item, index)}>{item}</li>)
3737
```
3838

39-
JS API might be NOT __SSR friendly__,
39+
JS API might be NOT (multi-tenant)__SSR friendly__,
4040

4141
#### React Components
42-
- `UID` - renderless container for generation Ids
42+
- (deprecated)`UID` - renderless container for generation Ids
43+
- `UIDConsumer` - renderless container for generation Ids
4344
```js
4445
import {UID} from 'react-uid';
4546

@@ -80,8 +81,8 @@ The difference between `uid` and `UID` versions are in "nesting" - any `UID` use
8081

8182
UID might be NOT __SSR friendly__,
8283

83-
#### Hooks (16.7+)
84-
- `useUID()` will generate just a UID
84+
#### Hooks (16.8+)
85+
- `useUID()` will generate a "stable" UID
8586
- `useUIDSeed()` will generate a seed generator, you can use for multiple fields
8687
```js
8788
const Form = () => {

__tests__/__snapshots__/index.spec.tsx.snap

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,103 +3,103 @@
33
exports[`UID test UID 1`] = `
44
<div>
55
<span>
6-
1
6+
ruid-1
77
</span>
88
<span>
9-
2
9+
ruid-2
1010
11-
2-uid1
11+
ruid-2-uid1
1212
</span>
1313
</div>
1414
`;
1515

1616
exports[`UID test UID 2`] = `
1717
<div>
1818
<span>
19-
key-3
19+
key-ruid-3
2020
</span>
2121
<span>
22-
key-4
22+
key-ruid-4
2323
24-
key-4-uid2
24+
key-ruid-4-uid2
2525
</span>
2626
</div>
2727
`;
2828

2929
exports[`UID test UIDConsumer 1`] = `
3030
Array [
3131
<span>
32-
1
32+
ruid-1
3333
</span>,
3434
<span>
35-
2
35+
ruid-2
3636
37-
2-uid1
37+
ruid-2-uid1
3838
</span>,
3939
<span>
40-
5
40+
ruid-5
4141
</span>,
4242
]
4343
`;
4444

4545
exports[`UID test UIDConsumer 2`] = `
4646
Array [
4747
<span>
48-
1
48+
ruid-1
4949
</span>,
5050
<span>
51-
1
51+
ruid-1
5252
53-
1-uid1
53+
ruid-1-uid1
5454
</span>,
5555
<span>
56-
6
56+
ruid-6
5757
</span>,
5858
]
5959
`;
6060

6161
exports[`UID test UIDFork 1`] = `
6262
Array [
6363
<span>
64-
1-1
64+
ruid-1-1
6565
</span>,
6666
<span>
67-
1-2
67+
ruid-1-2
6868
</span>,
6969
<span>
70-
2-1
70+
ruid-2-1
7171
</span>,
7272
<span>
73-
2-2
73+
ruid-2-2
7474
</span>,
7575
<span>
76-
3-1
76+
ruid-3-1
7777
</span>,
7878
<span>
79-
4-1
79+
ruid-4-1
8080
</span>,
8181
]
8282
`;
8383

8484
exports[`UID test UIDFork uid 1`] = `
8585
Array [
8686
<span>
87-
1-1-uid1
87+
ruid-1-1-uid1
8888
</span>,
8989
<span>
90-
1-2-uid2
90+
ruid-1-2-uid2
9191
</span>,
9292
<span>
93-
1-3-uid1
93+
ruid-1-3-uid1
9494
</span>,
9595
<span>
96-
2-1-uid1
96+
ruid-2-1-uid1
9797
</span>,
9898
<span>
99-
2-2-uid2
99+
ruid-2-2-uid2
100100
</span>,
101101
<span>
102-
2-3-uid1
102+
ruid-2-3-uid1
103103
</span>,
104104
]
105105
`;

package.json

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-uid",
33
"version": "2.2.0",
4-
"description": "Render less container for ID generation",
4+
"description": "Render-less container for ID generation",
55
"sideEffects": false,
66
"main": "dist/es5/index.js",
77
"types": "dist/es5/index.d.ts",
@@ -39,11 +39,11 @@
3939
"@types/node": "10.5.2",
4040
"codecov": "^3.0.2",
4141
"flow-bin": "^0.76.0",
42-
"react": "^16.7.0-alpha.2",
43-
"react-dom": "^16.7.0-alpha.2",
42+
"react": "^16.8.0",
43+
"react-dom": "^16.8.0",
4444
"react-test-renderer": "^16.4.0",
4545
"size-limit": "^0.21.1",
46-
"ts-react-toolbox": "^0.1.21"
46+
"ts-react-toolbox": "^0.2.20"
4747
},
4848
"size-limit": [
4949
{
@@ -55,7 +55,16 @@
5555
"node": ">=8.5.0"
5656
},
5757
"peerDependencies": {
58-
"react": "^16.3.0"
58+
"@types/react": "^16.8.0",
59+
"react": "^16.8.0"
60+
},
61+
"peerDependenciesMeta": {
62+
"@types/react": {
63+
"optional": true
64+
}
65+
},
66+
"dependencies": {
67+
"tslib": "^1.10.0"
5968
},
6069
"keywords": [
6170
"react",

src/UIDComponent.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export class UID extends React.Component<UIDProps> {
1010
id: getId(this.props.idSource || counter)
1111
};
1212

13-
1413
prefixId(id: number | string) {
1514
const uid = (this.state.prefix + id);
1615
return String(this.props.name ? this.props.name(uid) : uid);

src/context.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export type IdSourceType = {
77
uid: (item: any, index?: number) => string,
88
}
99

10-
export const createSource = (prefix = ''): IdSourceType => ({
10+
export const createSource = (prefix = 'ruid-'): IdSourceType => ({
1111
value: 1,
1212
prefix: prefix,
1313
uid: generateUID()
@@ -22,7 +22,6 @@ export interface UIDProps {
2222
export const counter = createSource();
2323

2424
export const source = React.createContext(createSource());
25-
export const lastUsed = React.createContext("");
2625

2726
export const getId = (source: IdSourceType) => source.value++;
28-
export const getPrefix = (source?: IdSourceType) => source ? source.prefix : '';
27+
export const getPrefix = (source?: IdSourceType) => source ? source.prefix : 'ruid-';

src/hooks.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @ts-ignore
21
import * as React from "react";
32
import {counter, getId, getPrefix, IdSourceType, source} from "./context";
43

@@ -16,10 +15,10 @@ const generateUID = (context: IdSourceType) => {
1615
const useUIDState = () => {
1716
if (process.env.NODE_ENV !== "production") {
1817
if (!('useContext' in React)) {
19-
throw new Error('Hooks API requires React 16.7+');
18+
throw new Error('Hooks API requires React 16.8+');
2019
}
2120
}
22-
// @ts-ignore
21+
2322
return React.useState(generateUID(React.useContext(source)))
2423
};
2524

src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import {generateUID} from "./uid";
1+
import {generateUID, generatedUID as uid} from "./uid";
22
import {UIDReset, UIDConsumer, UIDFork} from "./Control";
33
import {useUID, useUIDSeed} from "./hooks";
44
import {UID} from "./UIDComponent";
55

6-
const uid = generateUID();
7-
86
export {
97
generateUID,
108
uid,

src/uid.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ export const generateUID = () => {
1919
};
2020

2121
return uid;
22-
}
22+
}
23+
24+
export const generatedUID = generateUID();

0 commit comments

Comments
 (0)