react-native - 无法在反应本机功能中读取“this”
问题描述
我正在尝试从 facebook 登录设置用户数据并将其存储在一个状态但发生错误。
脸书认证
_fbAuth(){
LoginManager.logInWithReadPermissions(['public_profile']).then(function(res){
if(res.isCancelled){
console.log('login cancelled.')
} else {
AccessToken.getCurrentAccessToken().then(tokenData => {
if(tokenData) {
const { accessToken, userID } = tokenData;
fetch('https://graph.facebook.com/v2.5/me?fields=email,name,friends&access_token=' + accessToken).then((response) =>
response.json()).then((json) => {
this.setState({ user_email: json.email })
}).catch(() => {
reject('ERROR GETTING DATA FROM FACEBOOK')
}
)
}
})
}
}, function(err){
console.log(err)
})
}
将数据注册到引发此错误的状态:
[TypeError: undefined is not a function (evalating '_this3.setState({ user_email: json.email })')]
我应该如何访问响应this
内部?fetch()
解决方案
在您的构造函数中添加此行以将组件上下文绑定到函数范围。
this._fbAuth = this._fbAuth.bind(this);
这应该工作!
推荐阅读
- html - 边缘浏览器上的 Parse.cloud.run 功能故障(尝试重定向页面)
- java - 如何在 spotbug 报告中标记误报
- kotlin - Kotlin 类型擦除 - 为什么仅在泛型类型上不同的函数可编译,而仅在返回类型上不同的函数不是?
- wordpress - WordPress 列出 AZ 索引中的所有/选定页面
- sql - Red Gate Oracle Schema 比较 v3 和 v4 不解释 UserObjectPrivileges
- c# - 如何使用 Xero API 更新联系人的批量支付帐户名称和详细信息
- c - 如何将天转换为年月周然后天
- javascript - 问题
关于角材料设计 - python - Qt3D:根据实体和相机之间的距离缩放实体大小
- pytorch - 在 PyTorch 中用张量索引多维张量