Skip to content

Commit d154127

Browse files
committed
Unmount React tree asynchronously
1 parent fac689e commit d154127

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

test/regressions/index.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -222,24 +222,34 @@ if (unusedBlacklistPatterns.size > 0) {
222222
);
223223
}
224224

225-
const viewerRoot = ReactDOMClient.createRoot(document.getElementById('test-viewer'));
225+
const viewerRoot = document.getElementById('test-viewer');
226226

227227
function FixtureRenderer({ component: FixtureComponent }) {
228+
const viewerReactRoot = React.useRef(null);
229+
228230
React.useLayoutEffect(() => {
229-
const children = (
230-
<TestViewer>
231-
<FixtureComponent />
232-
</TestViewer>
233-
);
231+
const renderTimeout = setTimeout(() => {
232+
const children = (
233+
<TestViewer>
234+
<FixtureComponent />
235+
</TestViewer>
236+
);
234237

235-
viewerRoot.render(children);
236-
}, [FixtureComponent]);
238+
if (viewerReactRoot.current === null) {
239+
viewerReactRoot.current = ReactDOMClient.createRoot(viewerRoot);
240+
}
241+
242+
viewerReactRoot.current.render(children);
243+
});
237244

238-
React.useLayoutEffect(() => {
239245
return () => {
240-
viewerRoot.unmount();
246+
clearTimeout(renderTimeout);
247+
setTimeout(() => {
248+
viewerReactRoot.current.unmount();
249+
viewerReactRoot.current = null;
250+
});
241251
};
242-
}, []);
252+
}, [FixtureComponent]);
243253

244254
return null;
245255
}

0 commit comments

Comments
 (0)