如何在componentWillUnmount上停止Firestore`get`查询

问题描述 投票:1回答:1

好吧所以我在componentDidMount中从Firestore获取数据但是如果我更改组件它正在获取数据我得到错误说:

警告:无法在卸载的组件上调用setState(或forceUpdate)。这是一个无操作,但它表示应用程序中存在内存泄漏。要修复,请取消componentWillUnmount方法中的所有订阅和异步任务。

在Firebase实时数据库中,我们调用ref.off()来停止查询。

想知道如何在Firestore中做到这一点

componentDidMount() {
  Users.get().then(({ docs }) => {
    const users = docs.map(user => user.data());
      this.setState({ users });
  });
}

componentWillUnmount(){

}
javascript firebase google-cloud-firestore
1个回答
1
投票

由于Get不是实时方法,因此无法在componentWillUnmont上停止它,因此在卸载组件后不应调用它。

© www.soinside.com 2019 - 2024. All rights reserved.