在 jquery 中调用 react this.function

Call react this.function inside jquery

我在 reactJS 的 componentWillMount 上有一个 Jquery 函数:

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

下面声明了一个箭头函数:

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

为什么我得到 this.myFunction() 不是函数错误?

您正在另一个未绑定到您的对象的函数中调用 this,因此更改了 this 的范围。

你可以试试

a) 使函数成为箭头函数:

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

b) 把外面this绑定到匿名函数

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

c) 创建一个变量持有 this

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