diff --git a/src/React/render.ts b/src/React/render.ts index 6f2aadd6..e27a715c 100644 --- a/src/React/render.ts +++ b/src/React/render.ts @@ -3,26 +3,33 @@ import { createRoot } from 'react-dom/client'; import type { Root } from 'react-dom/client'; const MARK = '__rc_react_root__'; +const MARK_ID = '__rc_react_root_id__'; // ========================== Render ========================== type ContainerType = (Element | DocumentFragment) & { [MARK]?: Root; + [MARK_ID]?: number; }; -export function render(node: React.ReactElement, container: ContainerType) { +export const render = (node: React.ReactElement, container: ContainerType) => { const root = container[MARK] || createRoot(container); root.render(node); container[MARK] = root; -} + container[MARK_ID] = (container[MARK_ID] || 0) + 1; +}; // ========================= Unmount ========================== -export async function unmount(container: ContainerType) { +export const unmount = async (container: ContainerType) => { + const root = container[MARK]; + const rootId = container[MARK_ID]; // Delay to unmount to avoid React 18 sync warning return Promise.resolve().then(() => { - container[MARK]?.unmount(); - - delete container[MARK]; + if (container[MARK] === root && container[MARK_ID] === rootId) { + root?.unmount(); + delete container[MARK]; + delete container[MARK_ID]; + } }); -} +};