bas*_*tti 1 push-notification react-native
我正在获取令牌,但无法将其设置为状态,也无法从 onRegister 方法调用任何函数
PushNotification.configure({
onRegister: function(token) {
alert(token.token) //works fine
//shows an error this setState is not a function
this.setState({token:token.token})
//shows an error this this.sendToken.. is not a function
this.sendTokenTOServer(token.token)
}
});
Run Code Online (Sandbox Code Playgroud)
小智 5
假设你有这样的组件结构,
state = {}
configurePushNotifications() {
PushNotification.configure({ .... });
}
sendTokenTOServer() {}
Run Code Online (Sandbox Code Playgroud)
既然要引用父类作用域的方法,就需要this如下图赋值然后使用,因为thisonRegister方法内部是指传递给PushNotification.configure()函数的argument对象的作用域。
configurePushNotifications = () => {
const that = this;
PushNotification.configure({
onRegister: function(token) {
alert(token.token)
that.setState({token:token.token})
that.sendTokenTOServer(token.token)
}
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1336 次 |
| 最近记录: |