Skip to content

fix(wujie-react): 修复 React StrictMode 下子应用不启动的问题 - #1119

Merged
yiludege merged 1 commit into
masterfrom
fix/react-strictmode-remount
Nov 13, 2026
Merged

yiludege merged 1 commit into
masterfrom
fix/react-strictmode-remount

Conversation

@yiludege

Copy link
Copy Markdown
Collaborator
Checklist
  • 提交符合commit规范
  • 浏览器实测(见下文)
  • 测试用例添加:wujie-react 目前没有单测基础设施,本次以真实浏览器复现验证
详细描述

wujie-react@2.1.0 在 React 18 <StrictMode>(开发模式)下不会启动子应用,容器一直空白。2.0.2 正常。

#1098 给组件加了 isUnmounted,在 componentWillUnmount 置为 true,用来拦住卸载后残留在 __WUJIE_QUEUE 里的 startApp。StrictMode 会在同一个实例上执行 挂载 → 卸载 → 再挂载,再挂载时标志没有被重置,execStartApp 直接返回:

componentDidMount    instance#1 isUnmounted=false
componentWillUnmount instance#1 isUnmounted=false
componentDidMount    instance#1 isUnmounted=true   ← 不再 startApp

修复:componentDidMount 开头把 isUnmounted 重置为 false。

Vue 2 / Vue 3 封装不会复用实例重新挂载,不受影响,未修改。

验证

React 18.3.1 + Chromium 127,子应用 HTML 延迟 500ms,每 50ms 采样容器内容:

场景 master 本 PR
StrictMode 一直空白,未创建 wujie-app 510ms 显示子应用,子应用脚本执行 1 次
StrictMode + 卸载再挂载 首次空白,第二次挂载新实例才显示 两次都正常显示
非 StrictMode + 卸载再挂载 正常 正常(与 master 一致)
加载中卸载(#1098 场景) 见下方说明 与 master 一致

待验证:React 19.2 <Activity> 隐藏再显示时可能同样在同一实例上卸载再挂载,本 PR 应同样覆盖,但未实测。

关联:#1111(该 issue 是直接调用 wujie-core 的 startApp 时的并发问题,与本 PR 不同;本 PR 保证改用 wujie-react 封装组件后 StrictMode 可用)

另外发现(不在本 PR 范围)

加载中卸载组件时,startApp 会抛出「此报错可以忽略,iframe主动中断主应用代码在子应用运行」(#54),被组件的 try/catch 吞掉,this.destroy 拿不到,兜底销毁不执行,卸载后仍残留 1 个 sandbox iframe。master 上即如此,与本 PR 无关,另行跟进。

Made with Cursor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant