Skip to content

Commit 3070ad9

Browse files
Use useId instead of React internals (#3254)
* Use `useId` instead of React internals These may break in any React release which may result in blocking the ecosystem from seemlessly upgrading to new React releases. We can use `useId` instead which is available since React 18. * inline `useId` hook No need to use `useStableCollectionKey` anymore * reformat comment * update changelog --------- Co-authored-by: Robin Malfait <[email protected]>
1 parent 025e115 commit 3070ad9

File tree

2 files changed

+5
-26
lines changed

2 files changed

+5
-26
lines changed

packages/@headlessui-react/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- Merge incoming `style` prop on `ComboboxOptions`, `ListboxOptions`, `MenuItems`, and `PopoverPanel` components ([#3250](https://github.com/tailwindlabs/headlessui/pull/3250))
1818
- Prevent focus on `<Checkbox />` when it is `disabled` ([#3251](https://github.com/tailwindlabs/headlessui/pull/3251))
1919
- Fix visual jitter in `Combobox` component when using native scrollbar ([#3190](https://github.com/tailwindlabs/headlessui/pull/3190))
20+
- Use `useId` instead of React internals (for React 19 compatibility) ([#3254](https://github.com/tailwindlabs/headlessui/pull/3254))
2021

2122
## [2.0.4] - 2024-05-25
2223

packages/@headlessui-react/src/utils/stable-collection.tsx

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ function createCollection() {
1818
}
1919

2020
let renders = list.get(key) ?? 0
21+
// FIXME: This is a side-effect during render. `release` is only called in
22+
// an effect cleanup so we may never release if we had to render multiple
23+
// times before commit e.g. when a sibling suspends.
2124
list.set(key, renders + 1)
2225

2326
let index = Array.from(list.keys()).indexOf(key)
@@ -49,33 +52,8 @@ export function useStableCollectionIndex(group: string) {
4952
let collection = React.useContext(StableCollectionContext)
5053
if (!collection) throw new Error('You must wrap your component in a <StableCollection>')
5154

52-
let key = useStableCollectionKey()
55+
let key = React.useId()
5356
let [idx, cleanupIdx] = collection.current.get(group, key)
5457
React.useEffect(() => cleanupIdx, [])
5558
return idx
5659
}
57-
58-
/**
59-
* Return a stable key based on the position of this node.
60-
*
61-
* @returns {symbol | string}
62-
*/
63-
function useStableCollectionKey() {
64-
let owner =
65-
// @ts-ignore
66-
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED?.ReactCurrentOwner?.current ?? null
67-
68-
// ssr: dev/prod
69-
// client: prod
70-
if (!owner) return Symbol()
71-
72-
// client: dev
73-
let indexes = []
74-
let fiber = owner
75-
while (fiber) {
76-
indexes.push(fiber.index)
77-
fiber = fiber.return
78-
}
79-
80-
return '$.' + indexes.join('.')
81-
}

0 commit comments

Comments
 (0)