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

New rule: no-unbound-method-callbacks #63

Closed
danvk opened this issue Jan 24, 2017 · 3 comments
Closed

New rule: no-unbound-method-callbacks #63

danvk opened this issue Jan 24, 2017 · 3 comments

Comments

@danvk
Copy link

danvk commented Jan 24, 2017

This is a mistake:

class MyComponent extends React.Component<{}, {}> {
  render() {
    return <button onClick={this.handleClick}>Click me!</button>;
    //                      ~~~~~~~~~~~~~~~~  passing unbound method
  }

  handleClick(e: any) {
    alert('You clicked!');
  }
}

There are many possible solutions, a few of which include:

  • onClick={this.handleClick.bind(this)}
  • onClick={e => this.handleClick(e)}
  • Adding this.handleClick = this.handleClick.bind(this); in the constructor

A lint rule would need to at least check for the last one to avoid false positives.

@adidahiya
Copy link
Contributor

this could potentially be a more general rule: palantir/tslint#259

@nchen63
Copy link

nchen63 commented Jan 24, 2017

this will be part of the next tslint release palantir/tslint#2089

@adidahiya
Copy link
Contributor

closing as this is available in tslint core rules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants