-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BUGFIX: Replace deprecated ReactDOM.render with createRoot (#25)
This PR addresses warnings appearing on the console by updating occurrences of the deprecated `ReactDOM.render` function to `createRoot`, as necessitated by newer versions of React (>18). To ensure proper order of mounting and unmounting new roots and to avoid potential race conditions, a workaround utilizing `setTimeout` with a time of 0 was implemented. This workaround synchronizes the mount and unmount operations, aligning with the asynchronous behaviour of `createRoot`.
- Loading branch information
1 parent
d3619ad
commit c929670
Showing
16 changed files
with
124 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** @jest-environment jsdom */ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
import { screen } from "@testing-library/react"; | ||
import { act } from "react-dom/test-utils"; | ||
|
||
import { createSyncRoot } from "@foxglove/studio-base/panels/createSyncRoot"; | ||
|
||
describe("createSyncRoot", () => { | ||
it("should mount the component", async () => { | ||
const textTest = "Mount Component Test"; | ||
const TestComponent = () => <div>{textTest}</div>; | ||
|
||
const container = document.createElement("div"); | ||
document.body.appendChild(container); | ||
|
||
act(() => { | ||
createSyncRoot(<TestComponent />, container); | ||
}); | ||
|
||
expect(await screen.findByText(textTest)).toBeDefined(); | ||
}); | ||
|
||
it("should unmount the component", async () => { | ||
const textTest = "Unmount Component Test"; | ||
const TestComponent = () => <div>{textTest}</div>; | ||
|
||
const container = document.createElement("div"); | ||
document.body.appendChild(container); | ||
|
||
act(() => { | ||
const unmountCb = createSyncRoot(<TestComponent />, container); | ||
unmountCb(); | ||
}); | ||
|
||
expect(JSON.stringify(await screen.findByText(textTest))).toBe("{}"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
|
||
import { createRoot } from "react-dom/client"; | ||
|
||
/** | ||
* Creates a synchronized root for rendering React components. | ||
* | ||
* This function is designed to centralize the creation of React roots | ||
* for rendering components within a given HTML element. It addresses | ||
* potential race conditions that may occur when mounting and unmounting | ||
* components synchronously within the React lifecycle. | ||
* | ||
* By using a `setTimeout` with a minimal delay of 0 milliseconds, this | ||
* function ensures that the rendering and unmounting operations occur | ||
* asynchronously, allowing React to complete its current rendering cycle | ||
* before proceeding with the next operation. This helps prevent race | ||
* conditions and warnings related to synchronously unmounting roots. | ||
* | ||
* This approach was required since ReactDOM.render() was replaced by createRoot().render. | ||
* | ||
* @param component The JSX element to be rendered within the root. | ||
* @param panelElement The HTML element to serve as the container for the root. | ||
* @returns A function to unmount the root when needed. | ||
*/ | ||
export function createSyncRoot(component: JSX.Element, panelElement: HTMLDivElement): () => void { | ||
const root = createRoot(panelElement); | ||
setTimeout(() => { | ||
root.render(component); | ||
}, 0); | ||
return () => { | ||
setTimeout(() => { | ||
root.unmount(); | ||
}, 0); | ||
}; | ||
} |
Oops, something went wrong.