简体   繁体   English

当使用react-native与react-relay时,参考节点模块的箭头函数中的自动绑定已断开

[英]Broken autobinding in arrow function for referenced node modules when using react-native with react-relay

I'm using react-native and react-relay, therefore I've the following .babelrc file: 我正在使用react-native和react-relay,因此我有以下.babelrc文件:

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

Adding a dependency which uses arrow functions in their components as the MKIconToggle from react-native-material-kit ( https://github.com/xinthink/react-native-material-kit ) doesn't get transpiled correctly and the this reference is lost/wrong. 添加使用其组件中的箭头函数的依赖项作为来自react-native-material-kit( https://github.com/xinthink/react-native-material-kit )的MKIconToggle无法正确转换并且此引用丢失/错了。

The original code which ultimately causes the error looks like the following: 最终导致错误的原始代码如下所示:

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

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

The affected code fragment in the error case: 错误情况下受影响的代码片段:

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);
      }
    };

The _this reference equals window, due to the usage of _this the _this2 is created and used but _this is still used in the arrow functions(_onLayout) _this引用等于窗口,由于_this的使用_this2被创建和使用但是_this仍在箭头函数中使用(_onLayout)

When I delete the babelrc file and the default runs I get the following transpiled JS and it works: 当我删除babelrc文件和默认运行时,我得到以下转换的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.

I'm not really certain what causes the issue, I can fix it by binding the function in the constructor but I'd rather not change code in dependencies directly. 我不确定导致问题的原因,我可以通过在构造函数中绑定函数来修复它,但我不想直接更改依赖项中的代码。 I've already tried to add various presets to the babel conf: es2015, stage-0, babel-preset-react-native-stage-0 and some others all without luck. 我已经尝试为babel conf添加各种预设:es2015,stage-0,babel-preset-react-native-stage-0和其他一些都没有运气。

Funny thing is this behaviour doesn't occur in all dependencies and also not in my own code, if I just write a single component with an arrow function in it and use the babelrc it still works. 有趣的是,这种行为不会发生在所有依赖项中,也不会出现在我自己的代码中,如果我只是在其中编写带箭头函数的单个组件并使用babelrc它仍然有效。

I'm not able to 100% reproduce this behaviour, I've seen it with other dependencies as well but it seems to come and go, although once it occurs it doesn't usually go away anymore. 我无法100%重现这种行为,我已经看到它与其他依赖关系,但它似乎来去,虽然一旦它发生它通常不会消失。

babel-preset-react-native-stage-0 did it in the end. babel-preset-react-native-stage-0最终做到了。 Not sure what was left in the cache or somewhere else but after clearing it all with: watchman watch-del-all rm -rf $TMPDIR/react-* rm -rf node_modules npm install npm start --reset-cache my project and all it's arrow functions work. 不知道在缓存或其他地方留下了什么,但清除了所有内容后: watchman watch-del-all rm -rf $TMPDIR/react-* rm -rf node_modules npm install npm start --reset-cache my project and all它的箭头功能起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM