简体   繁体   中英

Firebase: How to stop listening to snapshot

I have a javascript application that uses the onSnapshot listener to listen to changes in my firebase collection. The docs tell me to unsubscribe when I am no longer in need of the listener. In React I would do this in componentWillUnmount() but if I've set up the listener in a vanilla class (not React), how would I unsubscribe in this situation?

How I'm implementing it...

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

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

Seems to me that you'd just want to make a method on MyPresenter called "stop()" or something similar, which does the unsubscribe. Then, in your component, call presenter.stop() during its componentWillUnmount() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM