Querying for Most Recent Firestore Documents with React-Redux-Firebase
Contents
Objective
Sometimes you want to query for the most recent firestore documents in a certain collection. In my case I wanted to display all documents that were created less than 20 minutes ago. Here is how I did it.
Prepping Firestore Documents
First, add a “created” field to the documents in the interested collection using firestore.Timestamp.now()
|
|
Syncing Redux Store with Firestore
Then add a useFirestoreConnect to the relevant component in order to keep the redux state in sync with firebase. Notice how we add the queryParams to order by value and take the first 10 documents.
Finally we use the javascript filter function to pull only those documents where the difference between the current time and created time is less than 20 minutes.
|
|