0

Can someone please help me with this

  getFriendList () {

        let friends = null;

        FB.api('/me/taggable_friends',{
            fields: 'id,name,picture',
            limit: 10,
            access_token: this.state.fbResponse.authResponse.accessToken
        }, (response) => {
            friends = response.data.map(friend => {
                return(
                    <Friend key={friend.id} />
                );
            });
        }); 

        return friends;
  };

It is not returning the friends list. Always returns null. please help.

I'm getting 10 data. When I do console.log, It is printing.

Thanks in advance...

1

1 Answer 1

4

FB.api is asynchronous and this is why you are getting null as result.

Can't you save friends in your state?

getFriendList () {
  FB.api('/me/taggable_friends',{
    fields: 'id,name,picture',
    limit: 10,
    access_token: this.state.fbResponse.authResponse.accessToken
  }, (response) => this.setState({ friends }))
}

I guess you are calling this method on componentDidMount:

componentDidMount() {
  this.getFriendsList()
} 

and later render friends list upon completion:

render() {
  return(
    <div>
      {this.state.friends && this.state.friends.map(friend =>
        <Friend key={friend.id} />
      )}
    </div>
  )
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your response. Previously I'm using this function in render function. So I cannot use setState. Now I changed with DidMount & working.
componentDidMount is the recommended place to fetch data from remote api as well described in ReactJS docs. Calling it on render is not a good practice though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.