react-native:嵌套回调如何绑定方法

时间:2017-03-06 18:48:50

标签: reactjs react-native react-native-ios

嵌套回调函数我尝试使用箭头函数,绑定函数,但在获取facebook访问令牌后函数没有触发我需要用 this.handleApiCalls 调用api这里我的代码片段

    const FBSDK = require('react-native-fbsdk')
const {
    LoginButton,
    AccessToken
 } = FBSDK

 class LoginScreen extends React.Component {
        constructor () {
            super(props)
        }

        handleApiCalls = () => {
            alert("handle the api calls");
        }

  render () {
     return  <View style={{justifyContent: 'center',
          alignItems: 'center'}}>
                            <LoginButton
                                publishPermissions={['publish_actions']}
                                onLoginFinished={
    // first call back
                                    (error, result) => {
                                      if (error) {

                                        console.log('error:', error);
                                      } else if (result.isCancelled) {
                                        alert('login is cancelled.')
                                        console.log('login is cancelled:', result);
                                      } else {
    // second callback

    AccessToken.getCurrentAccessToken().then((data) => {
        console.log('Access_Token:'+data.accessToken.toString())

    // ================ how to call this function ===============
        this.handleApiCalls 

      })


                                      }
                                    }
                                  }
                          onLogoutFinished={() => alert('logout.')}/>
                        </View>
        }

}

1 个答案:

答案 0 :(得分:2)

如果console.log('Access_Token:'+data.accessToken.toString())正在运行,则只需将this.handleApiCalls更改为this.handleApiCalls()即可调用它。