Skip to content

Binding, ES6 classes, and onChange #3613

@skevy

Description

@skevy

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions