简体   繁体   English

我如何对 firebase 时间戳数据进行排序反向 reactjs

[英]how can i sort firebase timestamp data reverse reactjs

When I create new note, it orders old to new, but I want to order new to old (reverse it).当我创建新笔记时,它从旧到新排序,但我想从新到旧排序(反转它)。 How can i do this ?我怎样才能做到这一点 ?

my codes:我的代码:

const notesRef = useFirestore().collection('users').doc(uid).collection("notes");
  const {status, data} = useFirestoreCollection(notesRef.orderBy("timezone"));

and its image: (Here, it order like 1-2-3, but i want to order, 3-2-1, new to old)和它的形象:(在这里,它的顺序像 1-2-3,但我想点,3-2-1,从新到旧) 在此处输入图片说明

our return like map:我们的回报就像地图:

 {data?.docs?.map((d, index) => {
              return (<Note
                      key={index}
                      id={index}
                      title={d.data().title}
                      content={d.data().content}
                      onDelete={deleteNote}
                      onDocId={d.id}
                      timezone={d.data().timezone}
                    />);
            })}

There are a bunch of ways of doing this.有很多方法可以做到这一点。

  • Fetch the data in reversed order from firebase.从 firebase 以相反的顺序获取数据。 I am using 25 as an example.我以 25 为例。 You can choose the limit as per your requirements.您可以根据自己的要求选择限制。
notesRef.orderBy("timezone").limitToLast(25);
  • Reverse the data on the client-side反转客户端的数据
const {status, data} = useFirestoreCollection(notesRef.orderBy("timezone"));
console.log(data.docs.reverse())

To sort a query in descending order, you can pass a second parameter to orderBy .要按降序对查询进行排序,您可以将第二个参数传递给orderBy So:所以:

notesRef.orderBy("timezone", "desc")

Also see the Firebase documentation on ordering data .另请参阅有关订购数据的 Firebase 文档。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM