1

I'm implementing the Facebook SDK on my react project, however, I am new to React and I still don't grasp some concepts. I'm calling handleFBLogin when a user clicks on a button. This function then calls checkLoginState to continue my code's logic. I've already bound checkLoginState in the constructor using:

this.CheckLoginState = this.checkLoginState.bind(this);

I call this function on handleFBLogin, but checkLoginState doesn't seem to be called. I can see the yes on my console:

handleFBLogin() { console.log("yes") this.checkLoginState; } 

Here's the checkLoginState function:

checkLoginState(){ console.log("checloginstate") window.FB.getLoginStatus(function(response) { console.log(response); this.statusChangeCallback(response); }.bind(this)); } 

Why isn't it being called?

10
  • "yes" is being logged ? Commented Aug 30, 2018 at 9:11
  • Yeah, sorry I forgot to mention it. Commented Aug 30, 2018 at 9:12
  • this.checkLoginState() Commented Aug 30, 2018 at 9:12
  • Where are you invoking the method ? Commented Aug 30, 2018 at 9:13
  • 1
    In your binding you have "Check" with capital C and then with lower case. Only use one as it is case sensitive. Commented Aug 30, 2018 at 9:15

3 Answers 3

3

It's possible you think to call the function without using parentheses as you do in the event like onClick={this.yourMethod}. It works because the react will handle it internally.

But calling a function from a method to another, you need to call the function using parentheses:

this.checkLoginState() 

But wait! this will be undefined here if have not bind this. So, bind this inside your constructor:

this.handleFBLogin = this.handleFBLogin.bind(this) 

Alternatively, you may use public class method:

handleFBLogin = () => { this.checkLoginState() 
Sign up to request clarification or add additional context in comments.

1 Comment

Unfortuantely, it gives me a TypeError: this.checkLoginState is not a function
0

I think it is type error. You forgot to add '()' for calling a function.

handleFBLogin() { console.log("yes") this.checkLoginState(); } 

Comments

0

inside handleFBLogin() try () => this.checkLoginState(); . You need the parenthesis to invoke the method.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.