Infinite Scroll Pagination with Firestore

Fireship · Beginner ·📰 AI News & Updates ·8y ago

Key Takeaways

The video demonstrates how to implement infinite scroll pagination with Firestore and Angular, covering the creation of a generic service, directives, and observables to fetch data asynchronously based on scroll position.

Full Transcript

infinite scroll is a feature that can have a huge impact on the user experience by loading data asynchronously based on scroll position in this video I'm going to show you how to implement infinite scroll and angular with the firestore database but we're going to do it in a way that not only allows you to scroll down to load content but also to scroll up and load content in the opposite direction this has been a commonly requested feature and it would be used if you were doing something like Facebook Messenger or slack where you load a timeline of messages in Reverse we're going to accomplish this by creating a generic service in angular and it will not only retrieve the data but also tell us when we've reached the end of a collection and when we are loading new data if you're new to the channel make sure to subscribe and grab the source code for this project from angular firebase comm I'm going to be starting from a brand new angular app and it's only dependency is angular fire so make sure to go to the documentation and follow the setup instructions there then the next step is to create a directive called scrollable this is going to tell us whether or not the user is scrolled to either the top or bottom of the page we're going to do that by monitoring the scroll event on the element itself that'd be that container element and then we're going to omit our own custom event that will tell us whether or not it's the bottom or top you can create a custom event and angular by using the output decorator and event emitter so our custom event here is named scroll position then I'm going to add element ref to the constructor but I want to warn you that using element ref for direct Dom access can cause issues in angular with platform server and web worker we can address this issue by wrapping native elements in a try-catch block then provide a fallback such as a load more button when the Don doesn't actually exist first we'll set up a host listener on the element then listen to the scroll event so keep in mind that your host element needs to be something scrollable and we'll set up a function here and put our try-catch block inside of it then we need three values from the native element itself we need scroll top scroll height and offset height first we need to know if the user has reached the bottom of the element so we can do that by saying top greater than height minus offset minus one extra pixel when that condition is met we our event emitter send out a string value of bottom four top we can simply see if the scroll top is at zero before we go any further let's make sure this works in an actual component so I'm going to switch over to the app component then I'm going to import observable as well as the pagination service which we're going to create the next step then I am setting up an event handler here that will just console.log the event from our custom event in the directive all we need to do is set up a div and inside that div will add the scrollable directive and then for the event we use parentheses with the scroll position event and then we'll run our scroll handler and I'm going to add an extra div in here with some height to it just so we can actually scroll through this parent div if you watch the console to the right you'll see we either get a bottom or top event once we've reached the bottom or top of the element itself and before we get to firestore let's generate a component called loading spinner then head over to spin kit and choose your favorite spinner and just copy and paste the HTML and CSS into that component before we build this big complex pagination service I want to show you how it's going to be used in the component to paginate in fire story you need to send a new query for each new batch of data that you need the service I'm creating will allow you to make a query with some basic configuration options and this will populate an observable on the service that we can subscribe to the first argument to an it is the collection and the second is the field that you want to order by you can order the collection in Reverse and you can also prepend new items to the beginning which is what you would use if you're doing an upward infinite scroll after the initial query we'll listen for events in our scroll handler once we receive the bottom event we can just call the more method from the service and that will populate the next batch of items from firestore so the service we're building will give you infinite scroll with just two lines of code the service will also give us a data variable that's an observable array that we can loop over with the async pipe so we'll display some information about our boats in the database but the service also has two other variables that are important the first one will tell us once we've reached the end of a collection and that's the done variable so we can listen for that and then say I ran out of boats once we reach the end we have a another observable that will tell us whether or not a new query is loading so we can show a loading spinner for each new query and that also helps us prevent sending multiple unnecessary queries if we take a look in fire store quickly we just have a boat's collection that's in the route and each document has a URL and a year field so we'll be sorting by the year when we look in the app we get the initial loading spinner and then once we scroll down the loading spinner will reappear and about a second later we'll get some new data loaded now we're finally ready to start building the service we're going to need angularfire store and we're also going to make pretty heavy usage of rxjs behavior subjects next I'm going to set up an interface that defines the query configuration options so we have a required field of path which is the path to your collection and then we have the field that you want to order by then we'll set default values for limit reverse and prepend prepends the important one that's the one that you would use if you wanted to have the items at the beginning of the array instead of added to the end of the array next I'm going to set up a few behavior subjects the subjects are all private variables and they have underscores so you can keep track of them easily in the code done will tell us once we've reached the end of the collection loading will tell us when a query is in process and data is the last batch of information that has been retrieved from firebase then we'll set the query configuration as a variable so we can keep track of it between queries and that'll be typed to our query config interface then for each of these subjects we're going to set an observable that will be consumed by the component this will keep all of the data changes isolated to the service this part is technically optional but it is considered a good practice all we need to do is call as observable on the source data then the next step is to add angular firestore to the constructor then we're going to go through a lot of code here so I recommend checking out the main article for detailed explanations on each of these methods the first one we'll set up is a knit which is going to make an initial query as well as save our query configuration on the service it requires a path to the collection as well as a field to order by and then it has optional defaults here that you can override by passing them as an object so we'll set a default limit up to and then say reverse false and prepend false now we can use this object to make a reference to the firestore collection we're going to order by the query field if the reverse property is set to true then we'll go ahead and order it descending otherwise it'll be ascending then we'll go ahead and limit it by whatever set in the query configuration then I have a map and update method that I'm going to show you in just a second here but first I'm going to show you how we define the observable data that will actually be used in the front-end component so we'll take our source data and then we'll call the rxjs scan operator on it scan allows us to build a larger array over time if the query options are set to prepend then we want to take the new value and concatenate it to the beginning of the array and if it's not set then we just want to go ahead and concatenate it to the end scan is a pretty weird operator to learn so I recommend going to rxjs marbles and checking it out visually now let's take a look at that map and update method it takes the angularfire store collection and then Maps it down to the snapshot that we need and then sends it to our main data source if the collection is already done or loading we don't want to make the query so we'll just return out of the function but otherwise we'll set the loading state to true and then we'll call collection snapshot changes to paginate a cursor and firestore you have to use the document snapshot so what we're going to do is map down the array to the data that we want to show on the front end as well as the snapshot you'll see in a second here how we use the snapshot to offset all future queries then after we have this data mapped down we're going to check and see if the prepend option is set then we need to reverse each individual batch to maintain a consistent order going in Reverse there may be a better way to do this but I wasn't able to figure one out for the upward scroll when you're pre new items to a list after we have the values mapped down we'll go ahead and update them in our main data source at this point we can set our loading state to false and we can also check to see if that was an empty array at which point we know we have reached the end of the collection and this is going to be a one-off operation for each new query so we're going to say take one and subscribe this will end your real-time connection with firebase but pagination with real-time data introduces a whole other host of issues and trade-offs that you have to think about it's beyond the scope of this video but please reach out on slack if you want to talk about it now we're ready to set up a method to make secondary queries so first we need to know the cursor that we want to paginate from if we're a prepending we want the document snapshot from the first item in the last batch otherwise we want the last item in the last batch if the array is empty we want to return null otherwise we will get an error from firestorm the more method is going to make all subsequent queries first we need to get the cursor for the pagination and then we'll create a firestorm reference it's identical to the initial query except that it uses start after and passing that cursor as an argument this tells firestore where to offset the query from then once we have that set we can call a map and update and our data source will be updated that was a lot of code to get through in 10 minutes but make sure to check out the full article for detailed explanations on all these methods my goal is to give you a generic service that can work with any firestore collection and give you infinite scroll either upwards or downwards that's it for firestore infinite scroll with angular if this video helped you please like and subscribe and if you want twice as much content every week consider becoming a pro member at angular firebase comm you'll get exclusive content a free copy of my book and one on one project consulting thanks for watching and I'll see you next time [Music] [Applause] [Music]

Original Description

Use Angular and Firestore to add infinite scroll pagination to your app from scratch - both downward and upward. https://angularfirebase.com/lessons/infinite-scroll-firestore-angular/ Firestore: https://cloud.google.com/firestore/docs/ Angular Directives: https://angular.io/guide/attribute-directives
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Fireship · Fireship · 58 of 60

1 Angular 4 Development and Production Environments with Firebase
Angular 4 Development and Production Environments with Firebase
Fireship
2 OAuth with Angular and Firebase Tutorial
OAuth with Angular and Firebase Tutorial
Fireship
3 Anonymous Authentication with Angular and Firebase - Lazy Registration
Anonymous Authentication with Angular and Firebase - Lazy Registration
Fireship
4 Angular Router Guards for Firebase Users
Angular Router Guards for Firebase Users
Fireship
5 Angular Firebase CRUD App with NoSQL Database Tutorial
Angular Firebase CRUD App with NoSQL Database Tutorial
Fireship
6 Upload Files from Angular to Firebase Storage
Upload Files from Angular to Firebase Storage
Fireship
7 How to Deploy an Angular App to Firebase Hosting
How to Deploy an Angular App to Firebase Hosting
Fireship
8 Sharing Data between Components in Angular
Sharing Data between Components in Angular
Fireship
9 Loading Spinners for Asynchronous Firebase Data
Loading Spinners for Asynchronous Firebase Data
Fireship
10 Angular 4 Transactional Email with Google Firebase Cloud Functions
Angular 4 Transactional Email with Google Firebase Cloud Functions
Fireship
11 Firebase Database Rules Tutorial
Firebase Database Rules Tutorial
Fireship
12 Autocomplete Search with Angular4 and Firebase
Autocomplete Search with Angular4 and Firebase
Fireship
13 Reddit Inspired Upvoting System with Angular and Firebase NoSQL
Reddit Inspired Upvoting System with Angular and Firebase NoSQL
Fireship
14 Angular Drag-and-Drop File Uploads to Firebase Storage
Angular Drag-and-Drop File Uploads to Firebase Storage
Fireship
15 Text Translation with Firebase Cloud Functions onWrite and Angular 4
Text Translation with Firebase Cloud Functions onWrite and Angular 4
Fireship
16 Custom Usernames with Firebase Authentication
Custom Usernames with Firebase Authentication
Fireship
17 Twitter-Inspired Follow Unfollow Feature with Firebase and Angular 4
Twitter-Inspired Follow Unfollow Feature with Firebase and Angular 4
Fireship
18 Simple Pagination with Firebase and Angular 4
Simple Pagination with Firebase and Angular 4
Fireship
19 How to Connect Firebase Users to their Data - 3 Methods
How to Connect Firebase Users to their Data - 3 Methods
Fireship
20 Add Toast Message Notifications to your Angular App
Add Toast Message Notifications to your Angular App
Fireship
21 Facebook-Inspired Reactions System with Angular and Firebase
Facebook-Inspired Reactions System with Angular and Firebase
Fireship
22 Learn NgModule in Angular with Examples
Learn NgModule in Angular with Examples
Fireship
23 Lazy Loading Components in Angular 4
Lazy Loading Components in Angular 4
Fireship
24 Stripe Checkout Payments with Angular and Firebase - Part 1
Stripe Checkout Payments with Angular and Firebase - Part 1
Fireship
25 Process Stripe Payments with Firebase Cloud Functions - Part 2
Process Stripe Payments with Firebase Cloud Functions - Part 2
Fireship
26 Selling Digital Content in Angular with Stripe Payments - Part 3
Selling Digital Content in Angular with Stripe Payments - Part 3
Fireship
27 Angular 4 Full Text Search with Algolia - Part 1
Angular 4 Full Text Search with Algolia - Part 1
Fireship
28 Algolia with Firebase Cloud Functions - Part 2
Algolia with Firebase Cloud Functions - Part 2
Fireship
29 Firebase Phone Authentication in Angular 4
Firebase Phone Authentication in Angular 4
Fireship
30 Top 7 RxJS Concepts for Angular Developers
Top 7 RxJS Concepts for Angular Developers
Fireship
31 Learn Angular Animations with 5 Examples
Learn Angular Animations with 5 Examples
Fireship
32 Advanced Firebase Data Filtering (Multi-Property)
Advanced Firebase Data Filtering (Multi-Property)
Fireship
33 Realtime Maps with Mapbox + Firebase + Angular
Realtime Maps with Mapbox + Firebase + Angular
Fireship
34 Angular Reactive Forms with Firebase Database Backend
Angular Reactive Forms with Firebase Database Backend
Fireship
35 Send Push Notifications in Angular with Firebase Cloud Messaging
Send Push Notifications in Angular with Firebase Cloud Messaging
Fireship
36 Top 7 Ways to Debug Angular 4 Apps
Top 7 Ways to Debug Angular 4 Apps
Fireship
37 Infinite Scroll with Angular and Firebase
Infinite Scroll with Angular and Firebase
Fireship
38 Use TypeScript with Firebase Cloud Functions
Use TypeScript with Firebase Cloud Functions
Fireship
39 Realtime Graphs and Charts with Plotly and Firebase
Realtime Graphs and Charts with Plotly and Firebase
Fireship
40 Role-Based User Permissions in Firebase
Role-Based User Permissions in Firebase
Fireship
41 User Presence System in Realtime - Online, Offline, Away
User Presence System in Realtime - Online, Offline, Away
Fireship
42 Location-based Queries with GeoFire and Angular Google Maps
Location-based Queries with GeoFire and Angular Google Maps
Fireship
43 Angular ngrx Redux Quick Start Tutorial
Angular ngrx Redux Quick Start Tutorial
Fireship
44 Angular Ngrx Effects with Firebase Database
Angular Ngrx Effects with Firebase Database
Fireship
45 Progressive Web Apps with Angular
Progressive Web Apps with Angular
Fireship
46 Angular Ngrx with Firebase Google OAuth User Authentication
Angular Ngrx with Firebase Google OAuth User Authentication
Fireship
47 RxJS Quick Start with Practical Examples
RxJS Quick Start with Practical Examples
Fireship
48 Send SMS Text Messages with Twilio and Firebase
Send SMS Text Messages with Twilio and Firebase
Fireship
49 Firebase Database Performance Profiling
Firebase Database Performance Profiling
Fireship
50 Native Desktop Apps with Angular and Electron
Native Desktop Apps with Angular and Electron
Fireship
51 Subscription Payments with Stripe, Angular, and Firebase
Subscription Payments with Stripe, Angular, and Firebase
Fireship
52 Firestore with AngularFire5 Quick Start Tutorial
Firestore with AngularFire5 Quick Start Tutorial
Fireship
53 Angular HTTP Client Quick Start Tutorial
Angular HTTP Client Quick Start Tutorial
Fireship
54 Google Sign-In with Firestore Custom User Data
Google Sign-In with Firestore Custom User Data
Fireship
55 Star Review System from Scratch with Firestore + Angular
Star Review System from Scratch with Firestore + Angular
Fireship
56 Angular Chatbot with Dialogflow (API.ai)
Angular Chatbot with Dialogflow (API.ai)
Fireship
57 Learn @ngrx/entity and Feature Modules
Learn @ngrx/entity and Feature Modules
Fireship
Infinite Scroll Pagination with Firestore
Infinite Scroll Pagination with Firestore
Fireship
59 Faster Firestore via Data Aggregation
Faster Firestore via Data Aggregation
Fireship
60 Contentful - CMS for Angular Progressive Web Apps
Contentful - CMS for Angular Progressive Web Apps
Fireship

This video teaches how to implement infinite scroll pagination with Firestore and Angular, covering the creation of a generic service, directives, and observables to fetch data asynchronously based on scroll position. The video provides a step-by-step guide on how to set up infinite scroll pagination in an Angular application using Firestore. By following this video, viewers can learn how to create a seamless user experience by loading data asynchronously as the user scrolls.

Key Takeaways
  1. Create a generic service in Angular to retrieve data and tell when the end of a collection is reached or new data is being loaded
  2. Create a directive called scrollable to monitor the scroll event on the element and emit a custom event
  3. Set up a host listener on the element and listen to the scroll event
  4. Use the service to make a query with basic configuration options and populate an observable that can be subscribed to
  5. Subscribe to the observable to fetch new data when the user scrolls to the bottom or top of the element
  6. Define query configuration with path, field, and optional defaults
  7. Use rxjs scan operator to build observable data
  8. Map and update data source with Firestore collection snapshot
💡 The video highlights the importance of using a generic service to retrieve data and tell when the end of a collection is reached or new data is being loaded, making it easier to implement infinite scroll pagination in an Angular application.

Related Reads

Up next
Egg Prices Were Manipulated, DOJ Says
Money Instructor
Watch →