我正在迁移react原生的0.51.1到0.59.8。我正面临着Mobx的问题。
如果@可观察的装饰成员已经像这样分配了init值
@observable cnt = 0;
那就成功了。
但如果没有定义的话,
@observable cnt;
那就没用了。
我有许多未定义的可观察到的商店,它们在0.51.0中工作。
我想让它以未定义的方式工作。
在迁移过程中,Babel的装饰选项发生了变化。
// babel.config.js
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: [
['@babel/plugin-transform-flow-strip-types'],
['@babel/plugin-proposal-decorators', { 'legacy': true}],
['@babel/plugin-proposal-class-properties', { 'loose': true}],
],
}
//This doesn't work but worked in react-native 0.51.0
import { observable } from 'mobx';
export class Count {
@observable cnt;
constructor(initValue = 0) {
this.cnt = initValue;
}
add(){
this.cnt++;
}
}
//This works
import { observable } from 'mobx';
export class Count {
@observable cnt = 0;
constructor(initValue = 0) {
this.cnt = initValue;
}
add(){
this.cnt++;
}
}
发布于 2019-05-30 23:31:53
我明确地解决了这个声明undefined
的问题。
@observable cnt = undefined;
发布于 2020-03-02 23:11:28
我用Babel的打字本写了这个问题。罪魁祸首是这些babel插件:
['@babel/plugin-transform-flow-strip-types'],
['@babel/plugin-proposal-decorators', { legacy: true }],
看来,未初始化的类属性上的装饰器不会被这些插件调用。它们有可能干扰了类型脚本对类属性的处理。
我的babel.config.js现在看起来是这样的:
module.exports = {
plugins: [['@babel/plugin-proposal-decorators', { legacy: true }]],
presets: ['module:metro-react-native-babel-preset']
};
https://stackoverflow.com/questions/56290003
复制相似问题