react-native:嵌套回调如何绑定方法
react-native: Nested callbacks how to bind method
嵌套回调函数我尝试使用箭头函数、绑定函数但是在获取 facebook 访问令牌后函数没有触发我需要用 this.handleApiCalls[= 调用 api 14=] 这里是我的代码片段
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>
}
}
如果 console.log('Access_Token:'+data.accessToken.toString())
正常工作,则只需将 this.handleApiCalls
更改为 this.handleApiCalls()
即可调用它。
嵌套回调函数我尝试使用箭头函数、绑定函数但是在获取 facebook 访问令牌后函数没有触发我需要用 this.handleApiCalls[= 调用 api 14=] 这里是我的代码片段
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>
}
}
如果 console.log('Access_Token:'+data.accessToken.toString())
正常工作,则只需将 this.handleApiCalls
更改为 this.handleApiCalls()
即可调用它。