Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby,gatsby-plugin-image): fix createRoot on React 18 #32378

Merged
merged 2 commits into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions packages/gatsby-plugin-image/src/components/lazy-hydrate.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { MutableRefObject } from "react"
import { hydrate, render } from "react-dom"
// @ts-ignore - react 18 has createRoot
import { hydrate, render, createRoot } from "react-dom"
wardpeet marked this conversation as resolved.
Show resolved Hide resolved
import { GatsbyImageProps } from "./gatsby-image.browser"
import { LayoutWrapper } from "./layout-wrapper"
import { Placeholder } from "./placeholder"
Expand Down Expand Up @@ -87,14 +88,29 @@ export function lazyHydrate(

if (root.current) {
// Force render to mitigate "Expected server HTML to contain a matching" in develop
const doRender = hydrated.current || forceHydrate.current ? render : hydrate
doRender(component, root.current)
hydrated.current = true
if (createRoot) {
if (!hydrated.current) {
hydrated.current = createRoot(root.current)
}

// @ts-ignore react 18 typings
hydrated.current.render(component)
} else {
const doRender =
hydrated.current || forceHydrate.current ? render : hydrate
doRender(component, root.current)
hydrated.current = true
}
}

return (): void => {
if (root.current) {
render((null as unknown) as ReactElement, root.current)
if (createRoot) {
// @ts-ignore react 18 typings
hydrated.current.render(null)
} else {
render((null as unknown) as ReactElement, root.current)
}
}
}
}
2 changes: 1 addition & 1 deletion packages/gatsby/cache-dir/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ apiRunnerAsync(`onClientEntry`).then(() => {
document.body.append(indicatorMountElement)

if (renderer === ReactDOM.hydrateRoot) {
renderer(indicatorMountElement).render(
ReactDOM.createRoot(indicatorMountElement).render(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is not html to hydrate so we d a regular root

<LoadingIndicatorEventHandler />
)
} else {
Expand Down