Skip to content

Commit

Permalink
2072 Use of createRoot causing errors with React 17 (#2073)
Browse files Browse the repository at this point in the history
Signed-off-by: CTomlyn <[email protected]>
  • Loading branch information
tomlyn authored Jul 29, 2024
1 parent 5a0b5f3 commit cf33bb2
Showing 1 changed file with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
import React from "react";
import { createRoot } from "react-dom/client";
import ReactDOM from "react-dom";

import Logger from "../logging/canvas-logger.js";
import CanvasUtils from "./common-canvas-utils.js";
Expand Down Expand Up @@ -49,24 +50,38 @@ export default class SvgCanvasExternal {
}

renderExternalObject(jsx, container) {
if (!container.ccExtRoot) {
container.ccExtRoot = createRoot(container);
// createRoot only available in React v18
if (createRoot) {
if (!container.ccExtRoot) {
container.ccExtRoot = createRoot(container);
}
container.ccExtRoot.render(jsx);

// Prior to React v18 we use ReatDOM.render
} else {
ReactDOM.render(jsx, container);
}
container.ccExtRoot.render(jsx);
}

removeExternalObject(obj, i, foreignObjects) {
const container = foreignObjects[i];
if (!container.ccExtRoot) {
container.ccExtRoot = createRoot(container);
// createRoot only available in React v18
if (createRoot) {
const container = foreignObjects[i];
if (!container.ccExtRoot) {
container.ccExtRoot = createRoot(container);
}
// Unmount in Timeout to stop this warning from appearing:
// "Warning: Attempted to synchronously unmount a root while
// React was already rendering."
setTimeout(() => {
container.ccExtRoot.unmount();
container.ccExtRoot = null;
});

// Prior to React v18 we use ReatDOM.unmountComponentAtNode
} else {
ReactDOM.unmountComponentAtNode(foreignObjects[i]);
}
// Unmount in Timeout to stop this warning from appearing:
// "Warning: Attempted to synchronously unmount a root while
// React was already rendering."
setTimeout(() => {
container.ccExtRoot.unmount();
container.ccExtRoot = null;
});
}

getActiveNodes() {
Expand Down

0 comments on commit cf33bb2

Please sign in to comment.