Skip to content

Commit a6fa7c7

Browse files
GatsbyJS BotLekoArts
GatsbyJS Bot
andauthored
fix(gatsby-plugin-image): Switch react-dom import to remove createRoot warning (#32457) (#32458)
(cherry picked from commit 9595ccb) Co-authored-by: Lennart <[email protected]>
1 parent e77af12 commit a6fa7c7

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

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

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, { MutableRefObject } from "react"
2-
// @ts-ignore - react 18 has createRoot
3-
import { hydrate, render, createRoot } from "react-dom"
2+
import ReactDOM from "react-dom"
43
import { GatsbyImageProps } from "./gatsby-image.browser"
54
import { LayoutWrapper } from "./layout-wrapper"
65
import { Placeholder } from "./placeholder"
@@ -88,28 +87,33 @@ export function lazyHydrate(
8887

8988
if (root.current) {
9089
// Force render to mitigate "Expected server HTML to contain a matching" in develop
91-
if (createRoot) {
90+
// @ts-ignore react 18 typings
91+
if (ReactDOM.createRoot) {
9292
if (!hydrated.current) {
93-
hydrated.current = createRoot(root.current)
93+
// @ts-ignore react 18 typings
94+
hydrated.current = ReactDOM.createRoot(root.current)
9495
}
9596

9697
// @ts-ignore react 18 typings
9798
hydrated.current.render(component)
9899
} else {
99100
const doRender =
100-
hydrated.current || forceHydrate.current ? render : hydrate
101+
hydrated.current || forceHydrate.current
102+
? ReactDOM.render
103+
: ReactDOM.hydrate
101104
doRender(component, root.current)
102105
hydrated.current = true
103106
}
104107
}
105108

106109
return (): void => {
107110
if (root.current) {
108-
if (createRoot) {
111+
// @ts-ignore react 18 typings
112+
if (ReactDOM.createRoot) {
109113
// @ts-ignore react 18 typings
110114
hydrated.current.render(null)
111115
} else {
112-
render((null as unknown) as ReactElement, root.current)
116+
ReactDOM.render((null as unknown) as ReactElement, root.current)
113117
}
114118
}
115119
}

0 commit comments

Comments
 (0)