Use the constructor only when necessary,
otherwise define public properties and methods in the class body.
- Reduce amount of code
- Autobinding
React doc - Handling events
React doc - Autobinding
class Basket {
constructor() {
this.count = 0;
this.handleAdd = this.handleAdd.bind(this);
}
handleAdd({add}) {
this.count += add;
}
}class Basket {
count = 0;
handleAdd = ({add}) => {
this.count += add;
};
}Back to Code Guide - JavaScript
Copyright © 2017 Stanislav Kochenkov