From 2e9fa7943d4bd47825bb3bf3f9e729354dadf073 Mon Sep 17 00:00:00 2001 From: lijianan <574980606@qq.com> Date: Sun, 17 May 2026 14:33:49 +0800 Subject: [PATCH 1/2] fix: avoid stale async unmount clearing latest React root --- src/React/render.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/React/render.ts b/src/React/render.ts index 6f2aadd6..c666d229 100644 --- a/src/React/render.ts +++ b/src/React/render.ts @@ -3,10 +3,12 @@ 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) { @@ -15,14 +17,19 @@ export function render(node: React.ReactElement, container: ContainerType) { root.render(node); container[MARK] = root; + container[MARK_ID] = (container[MARK_ID] || 0) + 1; } // ========================= Unmount ========================== export async function unmount(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]; + } }); } From b3c7c4078b0038dfea76a53f5fe173834d926cc8 Mon Sep 17 00:00:00 2001 From: lijianan <574980606@qq.com> Date: Sun, 17 May 2026 14:35:27 +0800 Subject: [PATCH 2/2] update --- src/React/render.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/React/render.ts b/src/React/render.ts index c666d229..e27a715c 100644 --- a/src/React/render.ts +++ b/src/React/render.ts @@ -11,17 +11,17 @@ type ContainerType = (Element | DocumentFragment) & { [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 @@ -32,4 +32,4 @@ export async function unmount(container: ContainerType) { delete container[MARK_ID]; } }); -} +};