在我的應用程式的螢屏上,我有一個useEffect鉤子可以檢索有關用戶的一些資訊以獲取通知,之后我想獲取按日期排序的所有通知陣列,問題是陣列似乎沒有排序:
useEffect(() => {
const arrayNotifications = [];
const arrayRequests = [];
notificationsAndRequests.forEach(async notif => {
const senderSnapshot = await database().ref(`/users/${notif.sender}`).once('value');
let senderImage = (senderSnapshot.val() && senderSnapshot.val().userImg);
if (notif.sender == null) {
const recieverSnapshot = await database().ref(`/users/${notif.sender}`).once('value');
senderImage = (recieverSnapshot.val() && recieverSnapshot.val().userImg);
}
const element = {
date: notif.date,
id: notif.id,
isInvitation: notif.isInvitation,
message: notif.message,
reciever: notif.reciever,
sender: notif.sender,
senderimage: senderImage,
timestamp: notif.timestamp,
title: notif.title,
extraid: notif.extraid,
requestid: notif.requestid,
viewed: notif.viewed,
status: notif.status || ''
};
if (notif.isInvitation) {
arrayRequests.push(element);
} else {
arrayNotifications.push(element);
}
});
const sortedNotifications = arrayNotifications.sort(
(a, b) => Date.parse(mom(b.timestamp, 'YYYY-MM-DD HH:mm').toDate())
- Date.parse(mom(a.timestamp, 'YYYY-MM-DD HH:mm').toDate())
);
const sortedRequests = arrayRequests.sort(
(a, b) => Date.parse(mom(b.timestamp, 'YYYY-MM-DD HH:mm').toDate())
- Date.parse(mom(a.timestamp, 'YYYY-MM-DD HH:mm').toDate())
);
setNotifications(sortedNotifications);
setRequestsNotifications(sortedRequests);
}, [notificationsAndRequests]);
兩者sortedRequests和sortedNotifications都應該按物件的timestamp屬性排序(只是一個格式化為日期的字串)。我應該同時排序嗎?notiftimestamparrayRequestsarrayNotifications
uj5u.com熱心網友回復:
async對你forEach來說不是等待arrayNotifications和arrayRequests結果。您需要將async邏輯包裝在另一個函式中,然后在那里更新您的狀態
const sortNotificationsAndRequests = async (notificationsAndRequests) => {
const arrayNotifications = [];
const arrayRequests = [];
for(const notif of notificationsAndRequests) {
const senderSnapshot = await database().ref(`/users/${notif.sender}`).once('value');
let senderImage = (senderSnapshot.val() && senderSnapshot.val().userImg);
if (notif.sender == null) {
const recieverSnapshot = await database().ref(`/users/${notif.sender}`).once('value');
senderImage = (recieverSnapshot.val() && recieverSnapshot.val().userImg);
}
const element = {
date: notif.date,
id: notif.id,
isInvitation: notif.isInvitation,
message: notif.message,
reciever: notif.reciever,
sender: notif.sender,
senderimage: senderImage,
timestamp: notif.timestamp,
title: notif.title,
extraid: notif.extraid,
requestid: notif.requestid,
viewed: notif.viewed,
status: notif.status || ''
};
if (notif.isInvitation) {
arrayRequests.push(element);
} else {
arrayNotifications.push(element);
}
}
const sortedNotifications = arrayNotifications.sort(
(a, b) => Date.parse(mom(b.timestamp, 'YYYY-MM-DD HH:mm').toDate())
- Date.parse(mom(a.timestamp, 'YYYY-MM-DD HH:mm').toDate())
);
const sortedRequests = arrayRequests.sort(
(a, b) => Date.parse(mom(b.timestamp, 'YYYY-MM-DD HH:mm').toDate())
- Date.parse(mom(a.timestamp, 'YYYY-MM-DD HH:mm').toDate())
);
setNotifications(sortedNotifications);
setRequestsNotifications(sortedRequests);
}
useEffect(() => {
//call the new function to update your states
sortNotificationsAndRequests(notificationsAndRequests)
}, [notificationsAndRequests]);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/456449.html
標籤:javascript 数组 反应 反应式
上一篇:查找陣列中連續值的運行和長度
