javascript - 监听器代理 Firebase 不仅在 ComponentDidMount 方法中

标签 javascript firebase react-native firebase-realtime-database

有没有办法在状态更改后设置数据库代理监听器?我的意思是通常我们过去常常在 componentDidMount 方法上设置它。但我想根据状态来调用它。

我希望我的用户获得特定数据的列表,而不是所有节点。该数据不应该直接在应用启动时“监听”,而是在应用进入特定状态后“监听”。

firebase.database().ref("myRef")
    .on("value", (snap)=>{ 
        console.log(snap.val()) 
    }
)

最佳答案

尝试定义一个函数并在状态发生变化时调用它,例如:

 functionForstateChange(){
    //call this function when you want to change your state.
   //setState here and make your required state changes if you need.
   this.functionForDatabase();    //call the function here
 }
 functionForDatabase(){
    //set your required database code here
 }

关于javascript - 监听器代理 Firebase 不仅在 ComponentDidMount 方法中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50197406/

相关文章:

javascript - 在不调用函数的情况下更改 ng-click 上的范围变量

javascript - 选中复选标记时增加 Div 高度

javascript - 如何在 javascript 中更改解析服务器中调用的优先级?

javascript - 如何在 Web 应用程序 Javascript 上检测用户首次登录 Firebase

android - 使用React Native在Android中使用bundle id打开其他应用程序?

javascript - 使用循环检查组件的样式

javascript - 使用 Webpack 基于环境的条件构建

javascript - 访问 firebase firestore v9 中的子集合

firebase - firestore 数据库类型验证

react-native - 如何在react-native中在外部播放器中打开视频网址