Skip to content

Commit 329051b

Browse files
committed
fix: replace React.DOM on newer react versions at showConfirmDialog
Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>
1 parent ecedf60 commit 329051b

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

src/components/mui/showConfirmDialog.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ import ReactDOM from "react-dom";
1515
import React from "react";
1616
import ConfirmDialog from "./confirm-dialog";
1717

18+
// React 18+ uses createRoot from react-dom/client; React 17 does not have this module
19+
let createRoot;
20+
try {
21+
({ createRoot } = require("react-dom/client"));
22+
} catch (_) {
23+
// React 17 — createRoot not available, will fall back to ReactDOM.render
24+
}
25+
1826
const showConfirmDialog = ({
1927
title,
2028
text,
@@ -28,16 +36,22 @@ const showConfirmDialog = ({
2836
const container = document.createElement("div");
2937
document.body.appendChild(container);
3038

39+
let root = null;
40+
3141
const close = (answer) => {
32-
ReactDOM.unmountComponentAtNode(container);
42+
if (root) {
43+
root.unmount();
44+
} else {
45+
ReactDOM.unmountComponentAtNode(container);
46+
}
3347
container.remove();
3448
resolve(answer);
3549
};
3650

3751
const handleConfirm = () => close(true);
3852
const handleCancel = () => close(false);
3953

40-
ReactDOM.render(
54+
const element = (
4155
<ConfirmDialog
4256
open
4357
title={title}
@@ -49,9 +63,15 @@ const showConfirmDialog = ({
4963
cancelButtonColor={cancelButtonColor}
5064
onConfirm={handleConfirm}
5165
onCancel={handleCancel}
52-
/>,
53-
container
66+
/>
5467
);
68+
69+
if (createRoot) {
70+
root = createRoot(container);
71+
root.render(element);
72+
} else {
73+
ReactDOM.render(element, container);
74+
}
5575
});
5676

5777
export default showConfirmDialog;

0 commit comments

Comments
 (0)