0

I have a Jquery function on my componentWillMount at reactJS:

$(document).ready(function(e) {
 this.myFunction();
}

And a arrow function declared down bellow:

myFunction = () => {
 alert("Jquery is ready!");
}

Why im getting this.myFunction() is not a function error?

Jacs
  • 1,437
  • 4
  • 21
  • 31

1 Answers1

0

You are calling this inside another function which it is not bound to your object and therefore changes the scope of this.

You can try

a) Make the Function an arrow function:

$(document).ready(() => this.myFunction());

b) Bind the outside this into the anonymous function

$(document).ready(function(e) {
 this.myFunction();
}.bind(this));

c) Create a variable holding this

var self = this;
$(document).ready(function(e) {
 self.myFunction();
});
halfer
  • 19,824
  • 17
  • 99
  • 186
marks
  • 1,501
  • 9
  • 24