Skip to content

Commit ed98bad

Browse files
authored
Use native useId and useSyncExternalStore hooks (#3092)
* use native `useSyncExternalStore` hook from React 18 * use native `useId` hook from React 18 * update changelog
1 parent 8652f80 commit ed98bad

File tree

6 files changed

+4
-239
lines changed

6 files changed

+4
-239
lines changed

packages/@headlessui-react/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3030
- Attempt form submission when pressing `Enter` on the `<Listbox.Button />` component ([#2972](https://github.com/tailwindlabs/headlessui/pull/2972))
3131
- Make the `Combobox` component `nullable` by default ([#3064](https://github.com/tailwindlabs/headlessui/pull/3064))
3232
- Deprecate the `entered` prop on the `Transition` component ([#3089](https://github.com/tailwindlabs/headlessui/pull/3089))
33+
- Use native `useId` and `useSyncExternalStore` hooks ([#3092](https://github.com/tailwindlabs/headlessui/pull/3092))
3334

3435
### Added
3536

Original file line numberDiff line numberDiff line change
@@ -1,24 +1,2 @@
1-
import React from 'react'
2-
import { env } from '../utils/env'
3-
import { useIsoMorphicEffect } from './use-iso-morphic-effect'
4-
import { useServerHandoffComplete } from './use-server-handoff-complete'
5-
6-
// We used a "simple" approach first which worked for SSR and rehydration on the client. However we
7-
// didn't take care of the Suspense case. To fix this we used the approach the @reach-ui/auto-id
8-
// uses.
9-
//
10-
// Credits: https://github.com/reach/reach-ui/blob/develop/packages/auto-id/src/index.tsx
11-
12-
export let useId =
13-
// Prefer React's `useId` if it's available.
14-
React.useId ??
15-
function useId() {
16-
let ready = useServerHandoffComplete()
17-
let [id, setId] = React.useState(ready ? () => env.nextId() : null)
18-
19-
useIsoMorphicEffect(() => {
20-
if (id === null) setId(env.nextId())
21-
}, [id])
22-
23-
return id != null ? '' + id : undefined
24-
}
1+
// Re-exporting the useId hook such that we can easily mock this hook in tests.
2+
export { useId } from 'react'

packages/@headlessui-react/src/hooks/use-store.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useSyncExternalStore } from '../use-sync-external-store-shim/index'
1+
import { useSyncExternalStore } from 'react'
22
import type { Store } from '../utils/store'
33

44
export function useStore<T>(store: Store<T, any>) {

packages/@headlessui-react/src/use-sync-external-store-shim/index.ts

-38
This file was deleted.

packages/@headlessui-react/src/use-sync-external-store-shim/useSyncExternalStoreShimClient.ts

-154
This file was deleted.

packages/@headlessui-react/src/use-sync-external-store-shim/useSyncExternalStoreShimServer.ts

-22
This file was deleted.

0 commit comments

Comments
 (0)