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.

Binding, ES6 classes, and onChange

See original GitHub issue

When using ES6 classes, autobinding doesn’t happen, as we all hopefully know by now 😃.

So when I do:

<button onClick={this.handleClick}>Test Button</button>

I’d expect that “this” inside of my handleClick function in my class is undefined. This is, in fact, what happens.

However, when I do:

<input onChange={this.handleChange} type="checkbox" />

…“this” inside of my handleChange function is the input component’s constructor.

Obviously, if I just bind my functions all the time, the problem is resolved. But since “this.setState” exists on the constructor…I was calling this.setState inside my handleChange function, not getting an error, and nothing was happening. So it was confusing to track down.

Admittedly I’m not immediately sure what to do about this…but I thought I’d make an issue and open it up for discussion.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:17 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
rsylviancommented, Sep 1, 2015

@Zenwolf you still need to .bind(this) or you can use the experimental ES7 function’s binding operator ::

onChange={::this.handleChange}
0reactions
ghostcommented, Jan 24, 2016

Alternatively, you may use the @autobind Decorator pattern from ES7 Stage 1 on the function using the autobind-decorator. Unlike function binding approach suggested by @MyBoon use of the decorator does not require use of the experimental Strawman (Stage 0) proposal.

The downside to using autobind-decorator is that it’s function-level only, so you may end up using it several times to decorate class methods as more of them require use of the class’ this context.

It seems to me the most favorable approach would be to use the class-level @autobind found in core-decorators if anything.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I bind onChange event to a function from an imported ...
In SearchBox.js, Employees is undefined. You should use Main.Employees instead. Second, filterEmployees is an instance method, not a class ...
Read more >
React Binding Patterns: 5 Approaches for Handling `this`
If you use an ES6 class, React no longer autobinds. One way to resolve this is to call bind in render: onChange={this.handleChange.bind(this)}.
Read more >
With React ES6 syntax class methods are not bind to ... - GitHub
The reason is that javascript's ES6 classes have no autobinding neither. React tries to not reinvent stuff that is already in javascript. ES5 ......
Read more >
React Class Components with ES6 and Class Fields - G2i
With the advent of ES6 and Class fields we can now write shorter components and without the need of worrying with the this...
Read more >
Two-way Binding Helpers - React
In React, you would implement this by listening to a “change” event, read from your data source (usually the DOM) and call setState()...
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