Skip to content

Commit

Permalink
build legacy root in React 16/17
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Nov 28, 2024
1 parent 8474b54 commit d926b40
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/renderWithoutAct.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as ReactDOMClient from 'react-dom/client'
import * as ReactDOM from 'react-dom'
import {
getQueriesForElement,
prettyDOM,
Expand Down Expand Up @@ -96,7 +97,11 @@ export type RenderWithoutActAsync = {
| Omit<RenderOptions, 'hydrate' | 'legacyRoot' | 'queries'>
| undefined,
): Promise<
RenderResult<SyncQueries, ReactDOMClient.Container, ReactDOMClient.Container>
RenderResult<
SyncQueries,
ReactDOMClient.Container,
ReactDOMClient.Container
>
>
}

Expand Down Expand Up @@ -141,8 +146,11 @@ export function renderWithoutAct(
let root: ReturnType<typeof createConcurrentRoot>
// eslint-disable-next-line no-negated-condition -- we want to map the evolution of this over time. The root is created first. Only later is it re-used so we don't want to read the case that happens later first.
if (!mountedContainers.has(container)) {
root = createConcurrentRoot(container)

root = (
ReactDOM.version.startsWith('16') || ReactDOM.version.startsWith('17')
? createLegacyRoot
: createConcurrentRoot
)(container)
mountedRootEntries.push({container, root})
// we'll add it to the mounted containers regardless of whether it's actually
// added to document.body so the cleanup method works regardless of whether
Expand All @@ -162,6 +170,17 @@ export function renderWithoutAct(
return renderRoot(ui, {baseElement, container, queries, wrapper, root: root!})
}

function createLegacyRoot(container: ReactDOMClient.Container) {
return {
render(element: React.ReactNode) {
ReactDOM.render(element as unknown as React.ReactElement, container)
},
unmount() {
ReactDOM.unmountComponentAtNode(container)
},
}
}

function createConcurrentRoot(container: ReactDOMClient.Container) {
const root = withDisabledActEnvironment(() =>
ReactDOMClient.createRoot(container),
Expand Down

0 comments on commit d926b40

Please sign in to comment.