Firestore / React is unsubscribe necessary in componentWillUnmount

萝らか妹 提交于 2020-01-11 10:43:57

问题


I have a component that is at a particular route:

<Route path='/route-path' component={MyComponent} /> 

In this component all of my firestore listeners are created in componentDidMount like so:

componentDidMount() {
   var unsubscribe = db.collection(name)
       .where("query", "==", false)
       .onSnapshot((querySnapshot) => {
          // do something
       })
}

Is it necessary to unsubscribe this listener if the route changes? Something like:

componentWillUnmount() {
  unsusbscribe() // from componentDidMount
}

or is this handled automatically by route changes, i.e. the websocket closes the connection?


回答1:


The Firestore SDK has no implicit knowledge about ReactJS and/or its lifecycle events.

You'll need to unsubscribe in your own code. It is not done automatically.



来源:https://stackoverflow.com/questions/49868757/firestore-react-is-unsubscribe-necessary-in-componentwillunmount

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!