Skip to content

Commit

Permalink
chore: refresh code
Browse files Browse the repository at this point in the history
  • Loading branch information
thearnica committed Jul 31, 2022
1 parent 70b3734 commit bee9b70
Show file tree
Hide file tree
Showing 13 changed files with 4,385 additions and 7,194 deletions.
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

lint-staged
121 changes: 69 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,56 @@
UID
=======
# UID

[![Build Status](https://travis-ci.org/thearnica/react-uid.svg?branch=master)](https://travis-ci.org/thearnica/react-uid)
[![coverage-badge](https://img.shields.io/codecov/c/github/thearnica/react-uid.svg?style=flat-square)](https://codecov.io/github/thearnica/react-uid)
[![NPM version](https://img.shields.io/npm/v/react-uid.svg)](https://www.npmjs.com/package/react-uid)
[![Greenkeeper badge](https://badges.greenkeeper.io/thearnica/react-uid.svg)](https://greenkeeper.io/)
[![bundle size](https://badgen.net/bundlephobia/minzip/react-uid)](https://bundlephobia.com/result?p=react-uid)
[![downloads](https://badgen.net/npm/dm/react-uid)](https://www.npmtrends.com/react-uid)

To generate a _stable_ UID/Key for a given `item`, consistently between client and server, __in 900 bytes__.
To generate a _stable_ UID/Key for a given `item`, consistently between client and server, **in 900 bytes**.

⚠️ SSR: **Not compatible with Strict or Concurent mode**. Consider using _native_ `useId`(React 18) hook instead.

⚠️ __Not compatible with Strict or Concurent mode__. Consider using _native_ `useId`(React 18) hook instead.
> If your clientside is using StrictMode it will never match SSR-ed Ids due to double invocation
Example - https://codesandbox.io/s/kkmwr6vv47

## API

React UID provides 3 different APIs
- vanilla js API - `uid(item) -> key`

- vanilla js API - `uid(item) -> key`
- React Component, via renderProp based API - `<UID>{ id => <><label htmlFor={id}/><input id={id}/></>}</UID>`
- React Hooks - `useUID`

#### Javascript

- `uid(item, [index])` - generates UID for an object(array, function and so on), result could be used as React `key`.
`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`
`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`

```js
import {uid} from 'react-uid';
// objects
const data = [{a:1}, {b:2}];
data.map( item => <li key={uid(item)}>{item}</li>)
// unique strings
const data = ["a", "b"];
data.map( item => <li key={uid(item)}>{item}</li>)
// strings
const data = ["a", "a"];
data.map( (item, index) => <li key={uid(item, index)}>{item}</li>)
```

JS API might be NOT (multi-tenant)__SSR friendly__,
import { uid } from 'react-uid';

// objects
const data = [{ a: 1 }, { b: 2 }];
data.map((item) => <li key={uid(item)}>{item}</li>);

// unique strings
const data = ['a', 'b'];
data.map((item) => <li key={uid(item)}>{item}</li>);

// strings
const data = ['a', 'a'];
data.map((item, index) => <li key={uid(item, index)}>{item}</li>);
```

JS API might be NOT (multi-tenant)**SSR friendly**,

#### React Components

- (deprecated)`UID` - renderless container for generation Ids
- `UIDConsumer` - renderless container for generation Ids

```js
import {UID} from 'react-uid';

Expand All @@ -51,7 +59,7 @@ JS API might be NOT (multi-tenant)__SSR friendly__,
<Fragment>
<input id={id} />
<label htmlFor={id} />
</Fragment>
</Fragment>
)}
</UID>

Expand All @@ -64,33 +72,36 @@ JS API might be NOT (multi-tenant)__SSR friendly__,
</Fragment>
)}
</UID>

// UID also provide `uid` as a second argument
<UID>
{(_, uid) => (
data.map( item => <li key={uid(item)}>{item}</li>)
data.map( item => <li key={uid(item)}>{item}</li>)
)}
</UID>

// in the case `item` is not an object, but number or string - provide and index
<UID>
{(_, uid) => (
data.map( (item, index) => <li key={uid(item, index)}>{item}</li>)
data.map( (item, index) => <li key={uid(item, index)}>{item}</li>)
)}
</UID>
```

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.

UID might be NOT __SSR friendly__,
UID might be NOT **SSR friendly**,

#### Hooks (16.8+)

- `useUID()` will generate a "stable" UID
- `useUIDSeed()` will generate a seed generator, you can use for multiple fields

```js
import { useUID, useUIDSeed } from 'react-uid';

const Form = () => {
const uid = useUID();
const uid = useUID();
return (
<>
<label htmlFor={uid}>Email: </label>
Expand All @@ -100,7 +111,7 @@ const Form = () => {
}

const Form = () => {
const seed = useUIDSeed();
const seed = useUIDSeed();
return (
<>
<label htmlFor={seed('email')}>Email: </label>
Expand All @@ -110,44 +121,48 @@ const Form = () => {
)
}
```
Hooks API __is SSR friendly__,
Hooks API **is SSR friendly**,
### Server-side friendly UID
- `UIDReset`, `UIDConsumer`, `UIDFork` - SSR friendly UID. Could maintain consistency across renders.
They are much more complex than `UID`, and provide functionality you might not need.
They are much more complex than `UID`, and provide functionality you might not need.
The key difference - they are not using global "singlentone" to track used IDs,
The key difference - they are not using global "singlentone" to track used IDs,
but read it from Context API, thus works without side effects.
Next example will generate the same code, regardless how many time you will render it
```js
import {UIDReset, UIDConsumer} from 'react-uid';
<UIDReset>
<UIDConsumer>
{(id,uid) => (
<Fragment>
<input id={id} />
<label htmlFor={id} />
data.map( item => <li key={uid(item)}>{item}</li>)
</Fragment>
)}
</UIDConsumer>
</UIDReset>
```js
import { UIDReset, UIDConsumer } from 'react-uid';

<UIDReset>
<UIDConsumer>
{(id, uid) => (
<Fragment>
<input id={id} />
<label htmlFor={id} />
data.map( item => <li key={uid(item)}>{item}</li>)
</Fragment>
)}
</UIDConsumer>
</UIDReset>;
```
__UID__ is not 100% SSR friendly - use __UIDConsumer__.
**UID** is not 100% SSR friendly - use **UIDConsumer**.
### Code splitting
Codesplitting may affect the order or existence of the components, so alter
the `componentDidMount` order, and change the generated ID as result.
In case of SPA, this is not something you should be bothered about, but for SSR
this could be fatal.
Next example will generate consistent keys regardless of component mount order.
Each call to `UIDFork` creates a new branch of UIDs untangled from siblings.
Next example will generate consistent keys regardless of component mount order.
Each call to `UIDFork` creates a new branch of UIDs untangled from siblings.
```js
import {UIDReset, UIDFork, UIDConsumer} from 'react-uid';

Expand All @@ -163,18 +178,20 @@ import {UIDReset, UIDFork, UIDConsumer} from 'react-uid';
<UIDConsumer>
{ uid => <span>{uid} is unique </span>}
</UIDConsumer>
</UIDFork>
</UIDFork>
</UIDReset>
```
The hooks API only needs the `<UIDFork>` wrapper.
### So hard?
"Basic API" is not using Context API to keep realization simple, and React tree more flat.
# Types
Written in TypeScript
# Licence
MIT
MIT
3 changes: 3 additions & 0 deletions __tests__/__snapshots__/strict-mode.spec.tsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`uid hooks in strict mode test uid 1`] = `"<div><i>1 == 2 ?</i></div>"`;
Loading

0 comments on commit bee9b70

Please sign in to comment.