必须在componentWillUnmount中取消订阅Firestore/React [英] Firestore / React is unsubscribe necessary in componentWillUnmount

查看:51
本文介绍了必须在componentWillUnmount中取消订阅Firestore/React的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个位于特定路径的组件:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆