【发布时间】:2020-02-09 22:02:56
【问题描述】:
我正在使用 react native 和 firebase 开发一个测试应用程序,我想通过在应用程序关闭时更新用户文档中的字段来实现存在状态。但是,我不知道在哪里应用断开连接,但我认为它会在 componentWillUnmount 的 App.js 中。我对本机和firebase都有反应,如果我错了,请指导我。下面是我在 App.js 中实现它的方式,但它不起作用。另外,我已经展示了只与这个问题相关的代码。
App.js
import { Firebase } from './Fire';
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
offline: 'offline',
};
}
componentWillMount() {
Firebase.init();
}
async componentWillUnmount() {
const userUid = await Firebase.auth.currentUser.uid;
const docRef = Firebase.firestore.collection('users').doc(userUid);
const disconnectRef = docRef.onDisconnect().update({
presenceStatus: this.state.offline
});
}
【问题讨论】:
标签: reactjs firebase react-native google-cloud-firestore