fix:修复React Native环境下属性值中插值表达式带空格的问题#2162
Merged
hiyuki merged 1 commit intodidi:masterfrom Aug 18, 2025
Merged
Conversation
hiyuki
approved these changes
Aug 18, 2025
Collaborator
|
感谢PR贡献,已合入主干跟随最近版本发布。 |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
修复 React Native 环境下属性插值表达式带空格导致类型错误的问题
问题描述
在使用 Mpx 框架开发跨端应用drn时,当在 React Native 环境(ios/android/harmony)下使用带空格的插值表达式作为组件属性时,会出现数据类型错误的问题。
例如:
在小程序环境下,该表达式会被正确解析为数字类型(假设 testIndex 的值为 1),但在 React Native 环境下,由于属性值中包含空格,会被当作字符串处理,最终传递给子组件的是字符串 "1 " 而不是数字 1。
这会导致在 React Native 环境下组件接收的属性类型与预期不符,可能引发运行时错误或功能异常,且对于刚接触的同学来说,排查费事费力,可能未注意到有空格的情况。
原因分析
经过源码分析,发现问题出在
@mpxjs/webpack-plugin的模板编译器中。在处理属性值时,对于 React Native 环境没有对带空格的插值表达式进行特殊处理。在小程序环境中,运行时环境会自动忽略插值表达式
{{}}外的空格,正确解析表达式。但在 React Native 环境中,Mpx 需要将模板编译为 JavaScript 代码,编译过程更加严格,会将整个属性值当作字符串字面量处理。解决方案
在
processAttrs函数中增加针对 React Native 环境的特殊处理逻辑:isReact(mode)判断){{开头,以}}结尾)这样可以确保即使用户在插值表达式前后不小心添加了空格,也能正确解析表达式并保持数据类型不变。
变更文件
packages/webpack-plugin/lib/template-compiler/compiler.js测试建议
一个测试用例来验证这个修复:
官方demo中的index.mpx
官方demo中的list.mpx
如果传值正确的话,列表应该显示DD,不正确的话,展示 手机、电视、电脑。
修复后:列表应展示DD
兼容性说明
该修改只影响 React Native 环境下的属性处理逻辑,不会影响其他平台的正常功能,是一个向后兼容的改进。