Skip to content

feat: RN 新增 notifyDimensionsChange 配置,支持手动触发 dimensions 更新 - #2485

Closed
mackwang112 wants to merge 7 commits into
masterfrom
fix-customDimensions-type
Closed

mackwang112 wants to merge 7 commits into
masterfrom
fix-customDimensions-type

Conversation

@mackwang112

@mackwang112 mackwang112 commented May 11, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • 新增 notifyDimensionsChange 方法,允许手动通知框架重新计算 rpx、vw、vh、媒体查询、onResize 等依赖尺寸的功能
  • 不传参时默认使用当前全局 dimensions,并重新执行 customDimensions 转换逻辑
  • 顺带优化 customDimensions 类型定义,去除无实际约束意义的泛型参数 T
  • 对Dimensions获取到到信息进行clone,防止直接修改影响到下次Dimensions.get的内容

Test plan

  • 折叠屏/分屏环境下手动调用 notifyDimensionsChange 验证 rpx/媒体查询更新
  • 不传参调用验证 customDimensions 重新执行
  • 确认 TypeScript 类型检查通过

mackwang112 and others added 5 commits May 11, 2026 21:30
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@mackwang112 mackwang112 added the platform:RN Mpx 跨端输出 RN label May 12, 2026
mackwang112 and others added 2 commits September 3, 2026 15:29

@hiyuki hiyuki left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

基于提交 080c1b2 完成审查,发现 1 个运行时回归和 1 个文档同步问题,详见行内评论。

验证:3 个变更代码/类型文件 ESLint 通过;5 个隔离 Jest 对照与复现用例通过(包含对当前错误行为的复现断言,不代表回归已修复),确认重复转换问题以及正常通知、缓存刷新行为。未进行 RN 真机验证。

// 懒初始化:首次调用时将初始 dimensions 写入全局(触发 customDimensions 处理)
if (!dimensionsApplied) {
dimensionsApplied = true
applyDimensionsInfo(global.__mpxAppDimensionsInfo)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] 首次样式计算前发生尺寸通知,会重复应用 customDimensions

dimensionsApplied 只在 formatValue() 中置为 true。如果此前收到原生 Dimensions change 事件或调用 notifyDimensionsChange(),onDimensionsChange() 已经应用了 customDimensions,但这里仍会对转换后的全局尺寸再次执行转换。

已用 PR 与基线对照复现:配置 customDimensions 为 dimensions => { dimensions.screen.width /= 2 },原始宽度 800,通知后宽度为 400,首次计算 750rpx 时却得到 200;基线在相同原生事件顺序下正确得到 400。这会导致布局与媒体查询使用错误尺寸,也影响已有的原生尺寸事件路径。

建议在 applyDimensionsInfo() 中统一维护初始化标记,恢复原实现“任何入口应用尺寸后均视为已初始化”的语义,并补充尺寸事件发生在首次样式计算前的回归测试。


- 勿在 App 构造函数执行完成前依赖 `getApp()` 内业务字段已赋值完毕;与路由相关的初始化宜放在 `onLaunch` / `onShow`。
- `getCurrentPages()` 依赖 React Navigation 焦点与 `__mpxPagesMap`,与原生小程序栈细节不完全相同。
- `notifyDimensionsChange` 由框架在首次样式计算时自动注入,需在开始渲染后才可调用。

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] 修正 API 注入时机,并补齐用户文档

这里写的是“首次样式计算时自动注入”,但实现是在 styleHelperMixin.ios.js 模块加载时直接执行 global.notifyDimensionsChange = onDimensionsChange,与首次 formatValue() 调用无关,建议按实际实现修正说明。

此外,docs-vitepress/guide/rn/application-api.md 在本 PR 中仅删除空行,没有新增 notifyDimensionsChange 的使用说明,尚未满足仓库对新增公开 API 的文档同步要求。建议在折叠屏适配章节补充参数、无参时从 Dimensions.get 读取原始尺寸并重新执行 customDimensions 的行为,以及调用示例。

mackwang112 added a commit that referenced this pull request Sep 10, 2026
Integrate PR #2485 and address its review feedback.
@mackwang112

Copy link
Copy Markdown
Collaborator Author

已合并到 #2418

mackwang112 added a commit that referenced this pull request Sep 11, 2026
Integrate PR #2485 and address its review feedback.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

platform:RN Mpx 跨端输出 RN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants