react-native - 将 react-native 与 react-relay 结合使用时,引用节点模块的箭头函数中的自动绑定(bind)中断

标签 react-native ecmascript-6 babeljs arrow-functions relay

我正在使用 react-native 和 react-relay,因此我有以下 .babelrc 文件:

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

添加一个在其组件中使用箭头函数的依赖项,因为来自 react-native-material-kit (https://github.com/xinthink/react-native-material-kit) 的 MKIconToggle 无法正确转译,并且 this 引用丢失/错误。

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

_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 文件并默认运行时,我得到以下转译后的 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.

我不太确定是什么导致了这个问题,我可以通过在构造函数中绑定(bind)函数来修复它,但我不想直接更改依赖项中的代码。 我已经尝试向 babel conf 添加各种预设:es2015、stage-0、babel-preset-react-native-stage-0 和其他一些都没有成功。

有趣的是,这种行为不会出现在所有依赖项中,也不会出现在我自己的代码中,如果我只编写一个带有箭头函数的组件并使用 babelrc 它仍然有效。

我无法 100% 重现此行为,我也看到它与其他依赖项有关,但它似乎来来去去,尽管一旦发生它通常不会再消失。

最佳答案

babel-preset-react-native-stage-0 最终做到了。 不确定缓存中或其他地方还剩下什么,但在清除所有内容后: 守望者 watch-del-all rm -rf $TMPDIR/ react -* rm -rf 节点模块 npm 安装 npm 启动 --reset-cache 我的项目及其所有箭头函数都有效。

关于react-native - 将 react-native 与 react-relay 结合使用时,引用节点模块的箭头函数中的自动绑定(bind)中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37324044/

相关文章:

javascript - 为什么 instanceof 对 babel-node 下的 Error 子类实例不起作用?

javascript - Webpack JSX 无法通过 ES6 导入解析相关模块

javascript - 与 React Native 共享文件

ios - 仅在 iOS 10.3.2 中,UIImage 从图库返回空图像路径

java - React Native Amplify AWS 包与 firebase 冲突

javascript - 在批量插入期间有条件地抑制 SimpleSchema 的字段验证

javascript - Electron:有没有办法通过 ipc 访问/修改主进程数组

ios - WebStorm - React-Native App 模拟器不工作

javascript - 使用剩余参数将参数展平为一维数组

javascript - Babel 将 JSX 翻译成 JS