簡體   English   中英

Firebase:如何停止收聽快照

[英]Firebase: How to stop listening to snapshot

我有一個JavaScript應用程序,該應用程序使用onSnapshot偵聽器偵聽Firebase集合中的更改。 當我不再需要偵聽器時,文檔告訴我取消訂閱。 在React中,我將在componentWillUnmount()執行此操作,但是如果我在普通類(不是React)中設置了偵聽器,在這種情況下我將如何退訂?

我如何實現它...

class MyComponent extends React.Component {
  constructor() {
    super();
    this.presenter = new MyPresenter();
  }
  ...rest of component

class MyPresenter {
    constructor() {
      const listener = db.collection("cities")
        .onSnapshot(function () {});
    }
}

在我看來,您只想在MyPresenter上創建一個稱為“ stop()”或類似方法的方法, MyPresenter取消訂閱。 然后,在您的組件中,在其componentWillUnmount()期間調用presenter.stop() componentWillUnmount()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM