【问题标题】:child_added on stackNavigator dont fire componentWillUnmountstackNavigator 上的 child_added 不会触发 componentWillUnmount
【发布时间】:2019-03-24 01:45:23
【问题描述】:

StackNavigator 不会触发 componentWillUnmount 事件,因此两个监听器保持打开状态。

有什么方法可以捕获“OnTabChange”事件来停止监听?

如果用户从一个选项卡快速切换到另一个选项卡,这会是最佳选择吗?

我还考虑在 App.js 文件中创建一个侦听器,以便它发出一条消息(带有快照)并通过侦听器侦听的屏幕捕获此事件。

我想在 App.js 中做一个监听器,比如

firebase.authFirebaseListener = auth.onAuthStateChanged((user) => {
  if (user) {
    db.ref('users')
    .child(auth.currentUser.uid)
    .child('favorites')
    .on('child_added', snapshotFavorites => {
       ## Send a signal with snapshotFavorites
       ## to refresh data if current screen need it.
    })
    ....
  } 
}

componentWillUnmount() {
    db.ref('users').child(auth.currentUser.uid).child('favorites').off()
    db.ref('users').child(auth.currentUser.uid).off()
    db.ref('users').off()
  }

【问题讨论】:

    标签: react-native firebase-realtime-database


    【解决方案1】:

    我不确定这是否是同一件事,但我使用了react-navigation-is-focused-hoc 并向我想触发 ComponentWillMount 并检测更改的页面添加了一个侦听器。 给它看看,我按照它在 github 上的教程所说的方式使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-13
      • 1970-01-01
      • 2021-06-03
      • 1970-01-01
      • 1970-01-01
      • 2013-11-12
      • 1970-01-01
      相关资源
      最近更新 更多