Skip to content

Commit ea455c9

Browse files
committed
[ADD] useId hook
1 parent d0a2144 commit ea455c9

7 files changed

Lines changed: 42 additions & 4 deletions

File tree

apps/react-tools-demo/src/components/hooks/useId/useId.md

Whitespace-only changes.

apps/react-tools-demo/src/constants/components.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ export const COMPONENTS = [
3737
"useMemoDeepCompare",
3838
"useCallbackCompare",
3939
"useCallbackDeepCompare",
40-
"useLazyRef"
40+
"useLazyRef",
41+
"useId"
4142
],
4243
//EVENTS
4344
[
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# useId
2+
_useId_ hook polyfilled for React versions below 18: __not use for key prop__.
3+
4+
## API
5+
6+
```tsx
7+
useId()
8+
```
9+
10+
> ### Params
11+
>
12+
>
13+
>
14+
15+
> ### Returns
16+
>
17+
>
18+
> - _string_
19+
>

apps/react-tools-demo/src/markdown/useSyncExternalStore.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# useSyncExternalStore
2-
_useSyncExternalStore_ polyfilled for React versions below 18 ___only client side___.
2+
_useSyncExternalStore_ hook polyfilled for React versions below 18 ___only client side___.
33

44
## API
55

packages/react-tools/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
- [x] useMemoCompare
3737
- [x] useMemoDeepCompare
3838
- [x] useLazyRef
39+
- [x] useId
3940

4041
- __EVENTS__
4142
- [x] useEvents
@@ -71,7 +72,6 @@
7172
- [x] useMediaQuery
7273
- [x] useColorScheme
7374
- [x] useTitle (change document.title but also document.head.title nodeElement)
74-
- [ ] useId (polyfill???)
7575
- [ ] useNetwork
7676
- [ ] useOnline
7777
- [ ] useFullscreen (check browser compatibility)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { useId as legacy } from "react"
2+
import { useLazyRef } from ".";
3+
4+
let count = 0;
5+
const increment = () => {
6+
return `:${(count++).toString(32)}:`;
7+
}
8+
9+
/**
10+
* __`useId`__: _useId_ hook polyfilled for React versions below 18: __not use for key prop__.
11+
* @returns {string}
12+
*/
13+
function useIdPolyfilled(): string {
14+
const id = useLazyRef(increment);
15+
return id.current;
16+
}
17+
18+
export const useId = legacy === undefined ? useIdPolyfilled : legacy;

packages/react-tools/src/hooks/useSyncExternalStore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useSyncExternalStore as legacy, useCallback, useEffect, useRef, useState } from 'react';
22

33
/**
4-
* __`useSyncExternalStore`__: _useSyncExternalStore_ polyfilled for React versions below 18 ___only client side___.
4+
* __`useSyncExternalStore`__: _useSyncExternalStore_ hook polyfilled for React versions below 18 ___only client side___.
55
* @param {(onStoreChange: () => void) => ()=>void} subscribe - function to register a callback that is called whenever the store changes.
66
* @param {()=>Snapshot} getSnapshot - function that returns the current value of the store.
77
* @returns {Snapshot} result - state

0 commit comments

Comments
 (0)