2016-05-19 30 views
2

我使用反应天然和反应中继,因此我已经以下.babelrc文件:破碎的自动绑定使用反应天然与当反应中继

{ 
    "sourceMaps": "both", 
    "presets": [ 
    "./plugins/babelRelayPlugin", 
    "react-native" 
    ], 
    "passPerPreset": true 
} 

在react-native-material-kit(https://github.com/xinthink/react-native-material-kit)中添加使用组件中的箭头函数的依赖关系作为MKIconToggle时,不能正确地进行转译,并且此引用丢失/错误。

最终导致该错误的原代码如下所示:

_onLayout = (evt) => { 
    this._onLayoutChange(evt.nativeEvent.layout); 

    if (this.props.onLayout) { 
     this.props.onLayout(evt); 
    } 
    }; 

在错误情况下,受影响的代码片断:

d(55, function(global, require, module, exports) {var _this = this, 
    _jsxFileName = '.../node_modules/react-native-material-kit/lib/mdl/Ripple.js'; 
var Ripple = function (_Component) { 
    babelHelpers.inherits(Ripple, _Component); 

    function Ripple(props) { 
    babelHelpers.classCallCheck(this, Ripple); 

    var _this2 = babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(Ripple).call(this, props)); 

    _this2._onLayout = function (evt) { 
     _this._onLayoutChange(evt.nativeEvent.layout); 

     if (_this.props.onLayout) { 
     _this.props.onLayout(evt); 
     } 
    }; 

的_this参考值等于窗口,由于使用的_this的_this2创建和使用,但_this仍然箭头功能(_onLayout)

用来当我删除babelrc文件,默认运行我得到以下transpiled JS和它的作品:

__d(921, function(global, require, module, exports) {var jsxFileName='...../node_modules/react-native-material-kit/lib/mdl/Ripple.js'; 

Component=React.Component;var Animated=React.Animated;var View=React.View;var PropTypes=React.PropTypes;var Platform=React.Platform;var Ripple=function(_Component){ 
babelHelpers.inherits(Ripple,_Component); 
function Ripple(props){babelHelpers.classCallCheck(this,Ripple); 
var _this=babelHelpers.possibleConstructorReturn(this,Object.getPrototypeOf(Ripple).call(this, props)); 
_this._onLayout=function(evt){ 
_this._onLayoutChange(evt.nativeEvent.layout); 

if(_this.props.onLayout){ 
_this.props.onLayout(evt);}};_this. 

我不是真的一定是什么原因造成的问题,我可以在构造结合的功能修复它,但我宁愿不直接依赖关系更改代码。 我已经试过向babel conf添加各种预设:es2015,stage-0,babel-preset-react-native-stage-0和其他一切都没有运气。

有趣的是这种行为并不适用于所有的依赖关系发生,也不会在我自己的代码,如果我只是写在它的箭头功能的单一组件,并使用它仍然有效的babelrc。

我不能100%重现此问题,我和其他依赖它看作是很好,但它似乎来来去去,但一旦它发生时通常不会消失了。

+0

你解决了吗? –

回答

0

babel-preset-react-native-stage-0最终做到了。 不知道什么被留在缓存中或其他地方,但与清理这一切后: watchman watch-del-all rm -rf $TMPDIR/react-* rm -rf node_modules npm install npm start --reset-cache 我的项目,它的所有功能箭头工作。