屏幕没有更新,但在数据库更新时获取控制台日志
我正在构建后端应用程序,当下订单时,我得到控制台日志,但屏幕没有更新,后退按钮(反应导航)也没有更新,几秒钟后响应。
const GetdataUsers = async () => {
let grabbedData1 = [];
let customerList1 = [];
await firebase
.database()
.ref(`/serviceProvider/${user1.uid}/franchise/customers`)
.orderByKey()
.on("value", (snapshot) => {
customerList1.push(snapshot.val());
// setCustomerList(customerList1);
if (customerList1) {
Object.keys(customerList1).map(function (key) {
let y = customerList1[key];
Object.keys(y).map(function (key2) {
let x = y[key2]; // get all customer id
// From that id fetch all the orders they have
firebase
.database()
.ref(`/orders/${x}`)
.orderByKey()
.on("value", (snapshot, key) => {
grabbedData1.push(snapshot.val());
console.log("grabbedData1....",grabbedData1);
// Grab all orders from the customers and set it
setShowloading(false);
setOrders(grabbedData1);
});
})
});
}
});
};
useEffect(() => {
GetdataUsers();
}, []); // [orders] tried this way also
当在括号中下订单时,如上所示,后退按钮冻结并且没有得到任何更新。还尝试从 useEffect 内的函数和 setorder 返回数据,但陷入循环。
I am building backend app, when the order is placed I get console log but the screen doesn't get updated and also the back button(react navigation), response after few seconds.
const GetdataUsers = async () => {
let grabbedData1 = [];
let customerList1 = [];
await firebase
.database()
.ref(`/serviceProvider/${user1.uid}/franchise/customers`)
.orderByKey()
.on("value", (snapshot) => {
customerList1.push(snapshot.val());
// setCustomerList(customerList1);
if (customerList1) {
Object.keys(customerList1).map(function (key) {
let y = customerList1[key];
Object.keys(y).map(function (key2) {
let x = y[key2]; // get all customer id
// From that id fetch all the orders they have
firebase
.database()
.ref(`/orders/${x}`)
.orderByKey()
.on("value", (snapshot, key) => {
grabbedData1.push(snapshot.val());
console.log("grabbedData1....",grabbedData1);
// Grab all orders from the customers and set it
setShowloading(false);
setOrders(grabbedData1);
});
})
});
}
});
};
useEffect(() => {
GetdataUsers();
}, []); // [orders] tried this way also
When placing orders in bracket as shown above back button freezes and doesn't get any update . Also tried returning the data from function and setorder inside useEffect but stuck in a loop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在代码中看到的问题:
getDataUsers
函数中的数组变化。customerList
。我认为最好将其分成两个单独的操作或效果,一个用于处理用户的
uid
更改以设置当前的customerList
状态,第二个用于处理当customerList
状态更新时获取/更新订单状态。示例:
当用户更改时更新客户列表。
当客户列表更新时更新订单。
Issues I see with the code:
getDataUsers
function.customerList
.I think it would be better to split this into two separate actions, or effects, one to handle the user's
uid
changing to set the currentcustomerList
state, and a second to handle fetching/updating the orders state when thecustomerList
state updates.Example:
Update the customer list when user changes.
Update the orders when customer list updates.
您已经创建了一个异步函数,但没有使用await 关键字。此外,您还必须为加载程序创建一个状态,并有条件地渲染加载程序组件,直到获取所有数据为止。
You have made an async function but you haven't used the await keyword. Also you'll have to create a state for loader and conditionally render a loader component till you have fetched all the data.