简体   繁体   English

使用 React Hooks 限制 Firebase 次重新渲染

[英]Limit Firebase rerenders with React Hooks

I have a Firebase Realtime Database and I want to display the records by mapping a list.我有一个 Firebase 实时数据库,我想通过映射列表来显示记录。

So far I have:到目前为止我有:

useEffect(() => {
        dbRefObject.on('value', snap => getRecords(snap.val()))
      }, [dbRefObject, records])

And elsewhere I have:在其他地方我有:

export const dbRefObject = firebase.database().ref().child('record');

My getRecords() function is:我的 getRecords() function 是:

const getRecords = (snap) => {
        let _recordsMap= []
        for (let record in snap) {
            _recordsMap.push({[record] : snap[record]})
        }

I want some kind of behaviour like an unsubscribe() function returned by the useEffect, but I can't get this to work?我想要某种行为,例如 useEffect 返回的 unsubscribe() function,但我无法让它工作?

useEffect(() => {
        const unsubscribe = () => {dbRefObject.on('value', snap => getRecords(snap.val()))}
        return () => {
            unsubscribe()
        }
      }, [dbRefObject, records])

you need to call dbRefObject.off("value", originalCallback);你需要调用 dbRefObject.off("value", originalCallback);

check https://firebase.google.com/docs/database/admin/retrieve-data#section-detaching-callbacks检查https://firebase.google.com/docs/database/admin/retrieve-data#section-detaching-callbacks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应 firebase 钩子 - 不能使用限制/方法 - react firebase hooks - cant use limit/where methods React 触发重新渲染 - React triggering rerenders 为什么用户不坚持使用 Firebase 和 Firebase React Hooks 进行页面刷新? - Why is the user not persisted on a page refresh with Firebase and Firebase React Hooks? Firebase 使用 react-firebase-hooks 时查询未定义 - Firebase query undefined when using react-firebase-hooks 使用 firebase 数据库和反应挂钩设置和更新表单输入 - Setting and updating form inputs with firebase database and react hooks 清除钩子上的异步调用 -React Native/Firebase 我该怎么做? - Clear asynchronous calls on hooks -React Native/Firebase how I do? 使用 Firebase 反应挂钩创建用户时出现“警告:无法更新组件” - "Warning: Cannot update a component" when creating a user with Firebase react hooks React Native与Firebase:如何限制获取数据的次数 - React Native and Firebase: how to limit number of times data are fetched Firebase web v9 升级中断 react-firebase-hooks“useCollectionData()” - Firebase web v9 upgrade breaks react-firebase-hooks "useCollectionData()" 从 Firebase 检索数据、对其执行计算并在 React(挂钩)中显示它的正确方法是什么? - What is the correct way to retrieve data from Firebase, perform computations on it, and display it in React (hooks)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM