Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic interpolations that contain "this" fail under some circumstances #486

Closed
gnandretta opened this issue Sep 1, 2018 · 1 comment · Fixed by #536
Closed

Dynamic interpolations that contain "this" fail under some circumstances #486

gnandretta opened this issue Sep 1, 2018 · 1 comment · Fixed by #536
Labels

Comments

@gnandretta
Copy link

The interpolation below works in next v5 but since v6 (which uses babel 7) it throws a TypeError: Cannot read property 'props' of undefined.

export default class Index extends React.Component {
  static getInitialProps() {
    return { color: 'aquamarine' };
  }

  render() {
    return (
      <div>
        {[1,2].map(idx => <div key={idx}>{this.props.color}</div>)}
        <style jsx>{`
          div {
            background: ${this.props.color};
          }
        `}</style>
      </div>
    );
  }
}

Sample apps

Details

Below is the babel output of the class for both applications. Note how in next 5 both this.props.color expressions, that is, the style interpolation and the div children, are compiled to _this2.props.color. However, in next 7, the style interpolation is compiled to this.props.color while the div children is compiled to _this.props.color.

Compiled class in next 5 app

_createClass(Index, [{
  key: 'render',
  value: function render() {
    var _this2 = this;

    return __WEBPACK_IMPORTED_MODULE_1_react___default.a.createElement(
      'div',
      {
        className: __WEBPACK_IMPORTED_MODULE_0_styled_jsx_style___default.a.dynamic([['1028791522', [_this2.props.color]]]),
        __source: {
          fileName: _jsxFileName,
          lineNumber: 8
        }
      },
      [1, 2].map(function (idx) {
        return __WEBPACK_IMPORTED_MODULE_1_react___default.a.createElement(
          'div',
          { key: idx, className: __WEBPACK_IMPORTED_MODULE_0_styled_jsx_style___default.a.dynamic([['1028791522', [_this2.props.color]]]),
            __source: {
              fileName: _jsxFileName,
              lineNumber: 9
            }
          },
          _this2.props.color
        );
      }),
      __WEBPACK_IMPORTED_MODULE_1_react___default.a.createElement(__WEBPACK_IMPORTED_MODULE_0_styled_jsx_style___default.a, {
        styleId: '1028791522',
        css: 'div.__jsx-style-dynamic-selector{background:' + _this2.props.color + ';}\n/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInBhZ2VzL2luZGV4LmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQVNvQixBQUdxRCx3Q0FDMUMiLCJmaWxlIjoicGFnZXMvaW5kZXguanMiLCJzb3VyY2VSb290IjoiL1VzZXJzL2dhYnJpZWwvQ29kZS9uZXh0LTUtc3R5bGVkLWpzeC10aGlzIiwic291cmNlc0NvbnRlbnQiOlsiZXhwb3J0IGRlZmF1bHQgY2xhc3MgSW5kZXggZXh0ZW5kcyBSZWFjdC5Db21wb25lbnQge1xuICBzdGF0aWMgZ2V0SW5pdGlhbFByb3BzKCkge1xuICAgIHJldHVybiB7IGNvbG9yOiAnYXF1YW1hcmluZScgfTtcbiAgfVxuXG4gIHJlbmRlcigpIHtcbiAgICByZXR1cm4gKFxuICAgICAgPGRpdj5cbiAgICAgICAge1sxLDJdLm1hcChpZHggPT4gPGRpdiBrZXk9e2lkeH0+e3RoaXMucHJvcHMuY29sb3J9PC9kaXY+KX1cbiAgICAgICAgPHN0eWxlIGpzeD57YFxuICAgICAgICAgIGRpdiB7XG4gICAgICAgICAgICBiYWNrZ3JvdW5kOiAke3RoaXMucHJvcHMuY29sb3J9O1xuICAgICAgICAgIH1cbiAgICAgICAgYH08L3N0eWxlPlxuICAgICAgPC9kaXY+XG4gICAgKTtcbiAgfVxufVxuIl19 */\n/*@ sourceURL=pages/index.js */',
        dynamic: [_this2.props.color]
      })
    );
  }
}, {
  key: '__reactstandin__regenerateByEval',
  value: function __reactstandin__regenerateByEval(key, code) {
    this[key] = eval(code);
  }
}], [{
  key: 'getInitialProps',
  value: function getInitialProps() {
    return { color: 'aquamarine' };
  }
}]);

Compiled class in next 7 app

_createClass(Index, [{
  key: "render",
  value: function render() {
    var _this = this;

    return react__WEBPACK_IMPORTED_MODULE_1___default.a.createElement("div", {
      __source: {
        fileName: _jsxFileName,
        lineNumber: 8
      },
      className: styled_jsx_style__WEBPACK_IMPORTED_MODULE_0___default.a.dynamic([["1028791522", [this.props.color]]])
    }, [1, 2].map(function (idx) {
      return react__WEBPACK_IMPORTED_MODULE_1___default.a.createElement("div", {
        key: idx,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 9
        },
        className: styled_jsx_style__WEBPACK_IMPORTED_MODULE_0___default.a.dynamic([["1028791522", [this.props.color]]])
      }, _this.props.color);
    }), react__WEBPACK_IMPORTED_MODULE_1___default.a.createElement(styled_jsx_style__WEBPACK_IMPORTED_MODULE_0___default.a, {
      styleId: "1028791522",
      css: "div.__jsx-style-dynamic-selector{background:".concat(this.props.color, ";}\n/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInBhZ2VzL2luZGV4LmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQVNvQixBQUdxRCx3Q0FDMUMiLCJmaWxlIjoicGFnZXMvaW5kZXguanMiLCJzb3VyY2VSb290IjoiL1VzZXJzL2dhYnJpZWwvQ29kZS9uZXh0LTctc3R5bGVkLWpzeC10aGlzIiwic291cmNlc0NvbnRlbnQiOlsiZXhwb3J0IGRlZmF1bHQgY2xhc3MgSW5kZXggZXh0ZW5kcyBSZWFjdC5Db21wb25lbnQge1xuICBzdGF0aWMgZ2V0SW5pdGlhbFByb3BzKCkge1xuICAgIHJldHVybiB7IGNvbG9yOiAnYXF1YW1hcmluZScgfTtcbiAgfVxuXG4gIHJlbmRlcigpIHtcbiAgICByZXR1cm4gKFxuICAgICAgPGRpdj5cbiAgICAgICAge1sxLDJdLm1hcChpZHggPT4gPGRpdiBrZXk9e2lkeH0+e3RoaXMucHJvcHMuY29sb3J9PC9kaXY+KX1cbiAgICAgICAgPHN0eWxlIGpzeD57YFxuICAgICAgICAgIGRpdiB7XG4gICAgICAgICAgICBiYWNrZ3JvdW5kOiAke3RoaXMucHJvcHMuY29sb3J9O1xuICAgICAgICAgIH1cbiAgICAgICAgYH08L3N0eWxlPlxuICAgICAgPC9kaXY+XG4gICAgKTtcbiAgfVxufVxuIl19 */\n/*@ sourceURL=pages/index.js */"),
      dynamic: [this.props.color]
    }));
  }
}], [{
  key: "getInitialProps",
  value: function getInitialProps() {
    return {
      color: 'aquamarine'
    };
  }
}])
@giuseppeg
Copy link
Collaborator

thanks Gabriel, that's indeed a bug and has to do with AST nodes reusing. We probably don't clone some node. We had a similar issue before #470

@giuseppeg giuseppeg added the bug label Sep 1, 2018
ijjk added a commit to ijjk/styled-jsx that referenced this issue Jan 4, 2019
undefined inside of arrow functions vercel#486
giuseppeg pushed a commit that referenced this issue Jan 15, 2019
Fixes #486

Switching to visiting the `Program` path and then start a traversal manually to solve conflicts with other Babel plugins.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants