首页 > 解决方案 > 如何停止多个 Firebase 事件回调(值,快照)

问题描述

我是火力基地的新手。我想在用户单击任何 pushKey 时获取实时数据。当我单击另一个节点时,我也会获得以前的节点数据。我不知道在哪里使用 ref.off('value',listener)。请帮帮我。任何帮助将不胜感激

export const fetchCurrentCircleLocation = pushKey => { return async dispatch => {

让 ref = firebase.database().ref( Circles/${pushKey}/members);

let listener = await ref.on("value", snapshot => {
  // console.log("circleLocation", snapshot.val());
  dispatch({
    type: FETCH_CURRENT_GROUP_CIRCLE_LOCATION,
    payload: snapshot.val()
  });
});

}; };

标签: react-nativefirebase-realtime-databasereact-redux

解决方案


尝试使用一次。它只会遍历您的数据库项目一次。您不需要删除侦听器。

ref.once("value", function (snapshot) {
        snapshot.forEach((child) => {
            console.log(child.val())

        });
    });

推荐阅读