question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Arrow function class fields transform `this` to `undefined`

See original GitHub issue

🐛 bug report

I’m not sure whether this is a parcel or a babel issue but given the following Code

class DrawingArea extends React.Component<...> {
  private setCanvas = (canvas) => {
    this.setState(state => ({ ...state, canvas }));
  }
}

the transpiled result tries to call setState on undefined like so:

class DrawingArea extends _reactDefault.Component {
  setCanvas = canvas => {
    undefined.setState(state => ({ ...state, canvas }));
  };
}

This might be the same issue as this.

I can fix this issue for prod builds with a custom browserslist configuration but it will still occur in development builds.

🎛 Configuration (.babelrc, package.json, cli command)

package.json:

{
  "scripts": {
    "start": "parcel serve --hmr-port 1235 src/index.html",
    "build": "parcel build src/index.html --no-minify"
  },
  "devDependencies": {
    "@parcel/validator-typescript": "^2.0.0-nightly.488",
    "glob": "^7.1.6",
    "http-proxy-middleware": "^1.0.3",
    "parcel": "^2.0.0-nightly.486",
    "sass": "^1.26.5",
    "typescript": "^3.8.3"
  }
}

🤔 Expected Behavior

this should not be replaced with undefined in class fields as the scope is well defined (the class instance).

😯 Current Behavior

this is replaced with undefined.

💻 Code Sample

https://github.com/maxjoehnk/remote-party-games/tree/feature/broken-build/applications/web-client

The snippet above is from this file

🌍 Your Environment

Software Version(s)
Parcel 2.0.0-nightly.486
Node v15.4.0
npm/Yarn yarn 1.22.10
Operating System Linux

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
aminyacommented, Dec 31, 2020

Out of the topic of this issue but my recommendation is to not use anonymous functions when the function is named. Arrow functions are meant to be passed to other functions when they are unnamed. When you assign an anonymous function to a named variable, you have already named it, so this is not considered a good idea.

Also, from the performance perspective, doing this may prevent the browser engine to inline the calls to these lambdas because the arrow functions are usually calculated during runtime.

Benchmarks

About 50% slower than a free function and 10% slower than a method function. You may not notice it in a simple function that calls this.setState which probably will not be inlined anyway, but when inlining is important it can have a huge effect.

image https://jsbench.me/g4kjcq9j3s/1

cc: @rinick @maxjoehnk @mischnic

Read more comments on GitHub >

github_iconTop Results From Across the Web

Arrow function expressions - JavaScript - MDN Web Docs
An arrow function expression is a compact alternative to a traditional function expression, with some semantic differences and deliberate ...
Read more >
this is undefined inside arrow function - Stack Overflow
If this is undefined within an arrow function, it's undefined outside of the arrow as well. Arrow function simply capture the this of...
Read more >
How To Use Javascript Arrow Functions & This Keyword
This guide will discuss how to use the arrow function to write your JavaScript functions. Focusing on converting regular functions to arrow ...
Read more >
Understanding Arrow Functions in JavaScript | DigitalOcean
Arrow functions are a new way to write anonymous function expressions in JavaScript, and are similar to lambda functions in some other ...
Read more >
Why “this” gets undefined inside a function - Dev Genius
Before getting into the “this” keyword, let's first understand “undefined”. In simple words, if we try to understand, “undefined” is the absence of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found