Skip to content

Commit 7baca70

Browse files
Don’t render <Portal>s while hydrating (#3825)
We're currently rendering the portal target element when hydrating. This breaks SSR when using portals. We need to *not* render portals during hydration since portals (even the wrapper target) are not renderable on the server. Fixes #3824 cc @RobinMalfait
1 parent 5ef7395 commit 7baca70

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

packages/@headlessui-react/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
- Nothing yet!
10+
### Fixed
11+
12+
- Don’t render `<Portal>` while hydrating ([#3825](https://github.com/tailwindlabs/headlessui/pull/3825))
1113

1214
## [2.2.9] - 2025-09-25
1315

packages/@headlessui-react/src/components/portal/portal.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { useDisposables } from '../../hooks/use-disposables'
1818
import { useEvent } from '../../hooks/use-event'
1919
import { useOnUnmount } from '../../hooks/use-on-unmount'
2020
import { useOwnerDocument } from '../../hooks/use-owner'
21+
import { useServerHandoffComplete } from '../../hooks/use-server-handoff-complete'
2122
import { optionalRef, useSyncRefs } from '../../hooks/use-sync-refs'
2223
import { usePortalRoot } from '../../internal/portal-force-root'
2324
import type { Props } from '../../types'
@@ -94,6 +95,7 @@ let InternalPortalFn = forwardRefWithAs(function InternalPortalFn<
9495
let target = usePortalTarget(ownerDocument)
9596
let parent = useContext(PortalParentContext)
9697
let d = useDisposables()
98+
let ready = useServerHandoffComplete()
9799
let render = useRender()
98100

99101
useOnUnmount(() => {
@@ -107,7 +109,7 @@ let InternalPortalFn = forwardRefWithAs(function InternalPortalFn<
107109

108110
let ourProps = { ref: portalRef }
109111

110-
return !target
112+
return !target || !ready
111113
? null
112114
: createPortal(
113115
<div

0 commit comments

Comments
 (0)