我正在我的反应项目上实现Facebook SDK,但是,我是React的新手,我仍然没有掌握一些概念。当用户点击按钮时,我正在调用handleFBLogin
。然后,此函数调用checkLoginState
继续我的代码逻辑。我已经在构造函数中绑定了checkLoginState
:
this.CheckLoginState = this.checkLoginState.bind(this);
我在handleFBLogin
上调用了这个函数,但似乎没有调用checkLoginState。我可以在我的控制台上看到yes
:
handleFBLogin() {
console.log("yes")
this.checkLoginState;
}
这是checkLoginState
功能:
checkLoginState(){
console.log("checloginstate")
window.FB.getLoginStatus(function(response) {
console.log(response);
this.statusChangeCallback(response);
}.bind(this));
}
它为什么不被称为?
您可以考虑在不使用括号的情况下调用函数,就像在onClick={this.yourMethod}
这样的事件中一样。它起作用,因为反应将在内部处理它。
但是从一个方法调用一个函数到另一个函数,你需要使用括号调用该函数:
this.checkLoginState()
可是等等!如果没有绑定this
,this
将在此处未定义。所以,在构造函数中绑定它:
this.handleFBLogin = this.handleFBLogin.bind(this)
或者,您可以使用公共类方法:
handleFBLogin = () => {
this.checkLoginState()
我认为这是类型错误。你忘了添加'()'来调用一个函数。
handleFBLogin() {
console.log("yes")
this.checkLoginState();
}
在handleFBLogin()
内部试试() => this.checkLoginState();
。您需要括号来调用该方法。