一,现象
模拟器报错:
React Native: Unexpected token ‘:’. Expected a ‘)’ or a ‘,’ after a parameter declaration
二,解决办法
这里写的很清楚
https://github.com/babel/babel/issues/14139
I have the same problem. This is caused by #14105.
Because of https://github.com/facebook/metro/blob/v0.58.0/packages/metro-react-native-babel-preset/src/transforms/transform-symbol-member.js#L52 will transform Symbol.iterator, but the change of #14105 will cause transform is break, finally the rest flow grammar is reserved.
Please update metro-react-native-babel-preset from 0.58.0 to 0.59.0 to resolve it.
If it doesn't take effect, please try yarn start --reset-cache to restart app.
And other two solutions: (only use one of)
use resolutions of yarn to lock @babel/traverse to 7.16.7 (https://classic.yarnpkg.com/lang/en/docs/selective-version-resolutions/#toc-how-to-use-it) and run yarn to reinstall, finally run yarn start --reset-cache to restart app.
update babel.config.js of your project : (assume your metro-react-native-babel-preset is 0.58.0)
from:
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
};
to:
module.exports = {
presets: [['module:metro-react-native-babel-preset', {
unstable_disableES6Transforms: true
}]],
};
finally run yarn start --reset-cache to restart app.
主要是因为:
"metro-react-native-babel-preset": "^0.58.0",
我采用的是修改babel.config.js的方法,亲测可行