Jeepeng / react-native-xinge-push

信鸽推送React Native版,支持华为、小米、魅族官方推送通道
Apache License 2.0
78 stars 29 forks source link

Unmount 中再调用 registerForXG ? #3

Closed stevenxyj closed 7 years ago

stevenxyj commented 7 years ago

componentWillUnmount 中调用 XGPush.removeEventListener('register', this._onRegister); _onRegister 中调用 XGPush.registerForXG

unmount 中再用 registerForXG ? 有什么作用

我调试发现,销毁组件时 componentWillUnmount 执行,this._onRegister 并没有随即执行,而是下次组件再显示时,调用 componentDidMount 后,this._onRegister会执行两次,一次是 removeEventListener 时调用的

_onRegister(deviceToken) {
  Alert.alert('_onRegister', this.state.status+ ':' + deviceToken);
  XGPush.registerForXG(deviceToken);
}
componentDidMount() {
  this.state.status = 'mount';

  XGPush.addEventListener('register', this._onRegister);
  XGPush.addEventListener('message', this._onMessage);
  XGPush.addEventListener('notification', this._onNotification);
}

componentWillUnmount() {
  this.state.status = 'unmount';

  XGPush.removeEventListener('register', this._onRegister);
  XGPush.removeEventListener('message', this._onMessage);
  XGPush.removeEventListener('notification', this._onNotification);
}
Jeepeng commented 7 years ago

_onRegister是个回调函数,

// 监听register事件,当注册成功会调用 this._onRegister()
XGPush.addEventListener('register', this._onRegister);

// 移除监听
XGPush.removeEventListener('register', this._onRegister);

与js dom中的addEventListenerremoveEventListener类似:https://developer.mozilla.org/zh-CN/docs/Web/API/EventTarget/addEventListener