Skip to content

Commit 1b45c7b

Browse files
GatsbyJS Botwardpeet
andauthored
fix(gatsby,gatsby-plugin-image): fix createRoot on React 18 (#32378) (#32420)
Co-authored-by: Ward Peeters <[email protected]>
1 parent 0ae10bf commit 1b45c7b

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

packages/gatsby-plugin-image/src/components/lazy-hydrate.tsx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { MutableRefObject } from "react"
2-
import { hydrate, render } from "react-dom"
2+
// @ts-ignore - react 18 has createRoot
3+
import { hydrate, render, createRoot } from "react-dom"
34
import { GatsbyImageProps } from "./gatsby-image.browser"
45
import { LayoutWrapper } from "./layout-wrapper"
56
import { Placeholder } from "./placeholder"
@@ -87,14 +88,29 @@ export function lazyHydrate(
8788

8889
if (root.current) {
8990
// Force render to mitigate "Expected server HTML to contain a matching" in develop
90-
const doRender = hydrated.current || forceHydrate.current ? render : hydrate
91-
doRender(component, root.current)
92-
hydrated.current = true
91+
if (createRoot) {
92+
if (!hydrated.current) {
93+
hydrated.current = createRoot(root.current)
94+
}
95+
96+
// @ts-ignore react 18 typings
97+
hydrated.current.render(component)
98+
} else {
99+
const doRender =
100+
hydrated.current || forceHydrate.current ? render : hydrate
101+
doRender(component, root.current)
102+
hydrated.current = true
103+
}
93104
}
94105

95106
return (): void => {
96107
if (root.current) {
97-
render((null as unknown) as ReactElement, root.current)
108+
if (createRoot) {
109+
// @ts-ignore react 18 typings
110+
hydrated.current.render(null)
111+
} else {
112+
render((null as unknown) as ReactElement, root.current)
113+
}
98114
}
99115
}
100116
}

packages/gatsby/cache-dir/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ apiRunnerAsync(`onClientEntry`).then(() => {
194194
document.body.append(indicatorMountElement)
195195

196196
if (renderer === ReactDOM.hydrateRoot) {
197-
renderer(indicatorMountElement).render(
197+
ReactDOM.createRoot(indicatorMountElement).render(
198198
<LoadingIndicatorEventHandler />
199199
)
200200
} else {

0 commit comments

Comments
 (0)