Enforcing event handlers as class fields with arrow functions
See original GitHub issueI’d like to enforce defining event handlers as class fields with arrow functions.
Good:
class LoggingButton extends React.Component {
handleClick = () => {
console.log('this is:', this);
}
render() {
return (
<button onClick={this.handleClick}>Click me</button>
);
}
}
Bad:
class LoggingButton extends React.Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick = () => {
console.log('this is:', this);
}
render() {
return (
<button onClick={this.handleClick}>Click me</button>
);
}
}
Also bad:
class LoggingButton extends React.Component {
handleClick() {
console.log('clicked');
}
render() {
return (
<button onClick={this.handleClick}>Click me</button>
);
}
}
This is one of the workarounds mentioned in the React Handling events documentation:
If you are using the experimental public class fields syntax, you can use class fields to correctly bind callbacks
Please let me know if there are existing rules that cover this.
This rule is somewhat related to react/jsx-no-bind.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:9 (7 by maintainers)
Top Results From Across the Web
Enforcing event handlers as class fields with arrow functions
I'd like to enforce defining event handlers as class fields with arrow functions. Good: class LoggingButton extends React.
Read more >Can I use an arrow function as the callback for an event ...
This code will fire the event listener and execute the callback anytime the matching element is clicked. It will, however, throw an error,...
Read more >How does event handlers with arrow functions achieve context ...
As the MDN docs states. An arrow function does not have its own this; the this value of the enclosing execution context is...
Read more >JavaScript Arrow Functions: How, Why, When (and ... - ZenDev
JavaScript arrow functions are roughly the equivalent of lambda functions ... be used by event handlers but that stayed bound to the class....
Read more >Private class features - JavaScript - MDN Web Docs - Mozilla
Private fields include private instance fields and private static fields. Private fields are accessible on the class constructor from inside the ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@alexzherdev not exactly, because the “meat” of the function is still shared on the prototype, and thus optimized. The trivial bind-proxy can’t be avoided, but at least that way we’re minimizing the amount of un-shared code.
Class fields should forever not be class fields. If you are tempted to do that, do this instead: