在 componentWillUnmount 中需要取消订阅 Firestore/React [英] Firestore / React is unsubscribe necessary in componentWillUnmount
问题描述
我有一个位于特定路线的组件:
I have a component that is at a particular route:
<Route path='/route-path' component={MyComponent} />
在这个组件中,我所有的 firestore 监听器都是在 componentDidMount 中创建的,如下所示:
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
}
或者这是由路由更改自动处理的,即 websocket 关闭连接?
or is this handled automatically by route changes, i.e. the websocket closes the connection?
推荐答案
Firestore SDK 没有关于 ReactJS 和/或其生命周期事件的隐含知识.
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.
这篇关于在 componentWillUnmount 中需要取消订阅 Firestore/React的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!