【问题标题】:[React-Native-Firebase]How can i remove onMessage exist event?[React-Native-Firebase]如何删除 onMessage 存在事件?
【发布时间】:2017-12-22 03:08:08
【问题描述】:

我声明了一个 onMessage 函数来做一些事情。但是当我导航到另一个屏幕并想要删除 ComponentWillUnmount() 中的 FCM.onMessage() 时。我怎样才能删除它?

const rnfirebase = RNFirebase.initializeApp()

export const FCM = rnfirebase.messaging()

ComponentDidMount(){
    FCM.onMessage((notif)=>{

        //Do something

    })

}
ComponentWillUnmount(){
    //I want to remove here

}

【问题讨论】:

  • 你能分享更多这个函数是在哪里写的,你的意思是说文件和默认函数吗?
  • 我刚刚更新了代码

标签: react-native react-native-firebase


【解决方案1】:

如果您检查docs onMessage 返回一个函数,如果控制台记录该函数,您将看到类似这样的内容

ƒ () {
    return rnListener.remove();
  }

因此,当您调用 onMessage 返回的函数时,它将停止侦听,在您的情况下,代码将如下所示

const rnfirebase = RNFirebase.initializeApp()

export const FCM = rnfirebase.messaging()

componentDidMount(){
    this.notificationListener = FCM.onMessage(notif=>{
        //Do something
    })
}

componentWillUnmount(){
    this.notificationListener(); //This will remove the listener
}

【讨论】:

  • 虽然不建议在这里说谢谢,但我需要说...解决我的一天!谢谢!
猜你喜欢
  • 2021-04-06
  • 2018-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-15
相关资源
最近更新 更多