Optimize React Native with FlatLists and Native Webview

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the Building React Native Apps for Mac course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to Building React Native Apps for Mac, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Building React Native Apps for Mac

We have seen a lot of the basic components of React Native so far. In theory you can just jump in and start creating your own application, but there are of course a lot more advanced components with different APIs. I would like to take a look into two specific components that are going to be very useful as you go down the line and start creating your application. One of them is the flat list component. So far whenever we had to display a list of items we have just placed them in their UI and iterated through them. But this doesn't really work as long as you have a very long list of items. Once you start rendering 100, 200 or maybe 1000 elements then you cannot render all of them at the same time. Your application is not going to work. So in order to solve this React Native provides what's called a flat list. So let's just jump right into the code and we will go exploring the possibilities of the flat list as we move along. So first I'm going to import it from React Native and I'm just going to put it inside the component. So flat list takes a lot of properties. We're just going to create a basic example for now. So one of the required properties is the data. Right this is the array of items the flat list is going to iterate upon. Another component, another property you need to pass is the render item function. So per each element of the array the render item function is going to be involved with that element. So I'm just going to create a placeholder for now. We're going to explore it in a little bit. Then we need to pass a key extractor function. This is going to take the element that will be rendered and we just need to return a unique key. We'll just use the title. One very useful property is the style that's going to be applied to the list. And if you have noticed so far our application doesn't really scale as it grows , we're going to take care of that now. So we're going to pass a flex one property which means it's going to expand. It's going to take as much space as possible without pushing or without messing with other elements. But that also means that we need to also make our container expand to take as much of the view as possible. So that's quite easy to do. We just use flex on both of them. So now that we have that working now is the time to create our function or render book function. So this function is going to be passed an object and inside of the object is going to be an item property. And that's going to be each of the books. It's going to be a book proper from our array. So we're going to type this and let me just see what name is. So the name of the property we're looking for is list render item info. And we're going to use our iBook type. So we need to import our iBook type from our stores. Are you ready to care of this in our UI store? I'm exporting the type or the interface that we have created before. So I couldn't hear it just imported. So this is just going to return that. So now you see the layout has shifted a little bit, but the list is empty because I'm not returning anything yet. So inside of my render book function, I'm just going to take more or less the same contents that I had before. I'm just going to return that as a final element. And instead of book, I have my item, but actually I don't need the key anymore because now it's going to be extracted. It's abstracted away because the flat list is a redirection. And we have that. And there you go. So now you will see the differences. I can actually scroll this. I'm not scrolling the entire interface. I'm just scrolling this part of the UI. And in here, now I could have thousands of books. This doesn't mean that if each of one of the elements is quite heavy to render, it's going to make it faster. It's just not going to render a thousand of them at the same time. It's going to render whatever is visible, maybe a little bit more down or upward so that when the user scroll still feels smooth. But it's going to improve the performance of our component quite a bit. So that's very useful. That's one of the components you want to use. Sometimes you might want to use just a scroll view, right? If you don't have a dynamic list of elements and you just want the UI completely to scroll, what you want to use is a scroll view, which is just as the name says, it's just a scroll view. It doesn't have any virtualization, but it allows your entire, all the stuff that you put in there is going to now be scrollable. Great. Now, let's just take a look at another component, which is a different type of component. This is a dependency. So we're going to add React Native WebView. This is a native package. It has a native component. So we're going to do a poly install. And we're going to rebuild our application. Great. So now that we have that working, let me just, just to be sure, I'll just close it and start it again. So now that we have our WebView component, what we can do is we can just use it inside the four components. So I'm going to go to my book container and I'm going to import WebView from React Native WebView. And now to make our application a little bit more interesting, instead of just displaying the title of the book that was passed into the component, I'm actually going to mount an embedded WebView. Now the WebView takes a source object and within the source object I need a URI . So I'm just going to take, I'm going to do a Google search just to display something interesting to the user. If somebody is taking a look into our favorite books or maybe me myself and I want to get some news or whatever, I can just do a Google search. So I'm just going to save that and now let's see what happens. If I, wait, I made a mistake. This is supposed to go like this. Great. So now if I go into Lord of the Rings, we are right. I need to accept the Google terms and conditions, but it doesn't matter. There it is, right? So this, the WebView component is fairly easy, right? It just mounts a small WebView inside of our, your application. But like this, there are many components which also have a UI element. There are camera components, there are SVG components, there are more image mounting components and so on and so forth. So you can just search for packages and import them and if they have a UI part, you can just display them within your application. One important thing to know here is that not all of them support macOS at the time. All of them support iOS and Android. So you might have to check if the specific component you're looking for actually can run on macOS machines. But I think that's it for the basic part of our JavaScript. Oh, I feel... Yeah that's it, I'll look.