[英]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.