4 ms·
The more I learn JS the more I am worried that I am not using 'this' right. I swap back and forth between javascript/python and with JS it always feels like I
by jtchang 9y ago
The more I learn JS the more I am worried that I am not using 'this' right.
I swap back and forth between javascript/python and with JS it always feels like I am one step away from disaster if I accidentally forget to bind this.
- gcoda 9y agoI feel you, but sometimes, I am using var that = this just to be sure
- deleted 9y ago[deleted]
- exclusiv 9y agoSounds like you are using it right but yeah you can setup the bindings or do an arrow function. I've been preferring just setting up the bindings in the constructor over arrow functions. So for the author's example I'd just do: constructor (){ this.increment.bind(this); } Then the template can stay cleaner with just onClick={this.increment}