Skip to content

Commit d926b40

Browse files
committed
build legacy root in React 16/17
1 parent 8474b54 commit d926b40

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/renderWithoutAct.tsx

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as ReactDOMClient from 'react-dom/client'
2+
import * as ReactDOM from 'react-dom'
23
import {
34
getQueriesForElement,
45
prettyDOM,
@@ -96,7 +97,11 @@ export type RenderWithoutActAsync = {
9697
| Omit<RenderOptions, 'hydrate' | 'legacyRoot' | 'queries'>
9798
| undefined,
9899
): Promise<
99-
RenderResult<SyncQueries, ReactDOMClient.Container, ReactDOMClient.Container>
100+
RenderResult<
101+
SyncQueries,
102+
ReactDOMClient.Container,
103+
ReactDOMClient.Container
104+
>
100105
>
101106
}
102107

@@ -141,8 +146,11 @@ export function renderWithoutAct(
141146
let root: ReturnType<typeof createConcurrentRoot>
142147
// 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.
143148
if (!mountedContainers.has(container)) {
144-
root = createConcurrentRoot(container)
145-
149+
root = (
150+
ReactDOM.version.startsWith('16') || ReactDOM.version.startsWith('17')
151+
? createLegacyRoot
152+
: createConcurrentRoot
153+
)(container)
146154
mountedRootEntries.push({container, root})
147155
// we'll add it to the mounted containers regardless of whether it's actually
148156
// added to document.body so the cleanup method works regardless of whether
@@ -162,6 +170,17 @@ export function renderWithoutAct(
162170
return renderRoot(ui, {baseElement, container, queries, wrapper, root: root!})
163171
}
164172

173+
function createLegacyRoot(container: ReactDOMClient.Container) {
174+
return {
175+
render(element: React.ReactNode) {
176+
ReactDOM.render(element as unknown as React.ReactElement, container)
177+
},
178+
unmount() {
179+
ReactDOM.unmountComponentAtNode(container)
180+
},
181+
}
182+
}
183+
165184
function createConcurrentRoot(container: ReactDOMClient.Container) {
166185
const root = withDisabledActEnvironment(() =>
167186
ReactDOMClient.createRoot(container),

0 commit comments

Comments
 (0)