Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native with RxJS

I am using React Native with RxJS and up until now, whenever I subscribe to an observable I have been doing:

observable.subscribe(() => {
     this.setState({ loading: true });
}.bind(this));

But since I upgraded to React Native 0.16.0, everywhere I have performed bind(this) on an inline function declared with the ES2015 arrow notation, React Native picks it up as an error. However when I change the arrow notation back to ES5 regular function notation as below:

observable.subscribe(function() => {
    this.setState({ loading: true });
}.bind(this));

The errors seem to go away.

What is going on here?

like image 254
coldbuffet Avatar asked Dec 11 '15 05:12

coldbuffet


1 Answers

When you use an arrow function you're already binding this to that particular function. So:

() => {} === function() {}.bind(this)
like image 89
André Fatia Avatar answered Nov 18 '22 19:11

André Fatia