简体   繁体   English

如何使用 useEffect 将 firebase 实时数据库中的更改显示到我的页面?

[英]How can I show the change in firebase realtime database to my page with useEffect?

I am doing firebase realtime database operation using useEffect() in my react js project.我在我的 react js 项目中使用 useEffect() 进行 firebase 实时数据库操作。 But when the data changes, the change does not come to my page.但是当数据发生变化时,变化并没有出现在我的页面上。 What is the solution to this?解决方案是什么?

  const [rideRequest, setRideRequest] = useState({});

  // RİDE REQUEST
  useEffect(() => {
    fireDb
      .child(`All Ride Requests/${id}`)
      .get()
      .then((snapshot) => {
        setRideRequest({ ...snapshot.val() });
      });
  }, [id]);

You are using .get() that'll fetch data only once from the database and won't listen for any updates.您正在使用.get() ,它只会从数据库中获取一次数据,并且不会监听任何更新。 If you want real-time updates then you must use .on() as shown below:如果您想要实时更新,那么您必须使用.on() ,如下所示:

useEffect(() => {
  fireDb
    .child(`All Ride Requests/${id}`)
    .on('value', (snapshot) => {
      setRideRequest(snapshot.val());
    });
}, [id]);

Checkout the documentation for more information.查看文档以获取更多信息。

暂无
暂无

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

相关问题 如何从我的 Firebase 实时数据库的多个节点填充我的 RecyclerView? - How can I populate my RecyclerView from multiple nodes of my Firebase Realtime Database? 我如何将我的 firebase 实时数据库 function 转换为 firestore function - How can i convert my firebase realtime database function to firestore function 如何在React中更改实时数据库firebase URL - How to change firebase realtime database URL in React 如何使用规则禁止 Firebase 实时数据库中的用户? - How can I ban a User in Firebase realtime Database using the rules? 如何从 Firebase 实时数据库中删除一些数据? - How can I delete some data from Firebase Realtime Database? 如何在 flutter 中实时获取 firebase 数据库中孩子的价值? - how can i get the value of a child in firebase database realtime in flutter? 我正在尝试在 recycleview Kotlin 中显示我的 firebase 实时数据库项目 - I am trying to show my firebase realtime database items in recycleview Kotlin 如何限制 Firebase 实时数据库的结果显示最新的分页? - How to limit the results of Firebase Realtime Database to show most recent for pagination? 如何将我的数据(在键:值对中)推送到 android 中的 Firebase 实时数据库中? - How do I push my data (in key:value pair) into my Firebase realtime database in android? 如何在没有“default-rtdb”的情况下强制 Firebase 实时数据库 URL - How can I force Firebase Realtime Database URL without "default-rtdb"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM