Complete React Native Tutorial #25 - Using the FlatList Components

Net Ninja · Beginner ·🔧 Backend Engineering ·1y ago
Skills: React90%

Key Takeaways

This video tutorial demonstrates how to use the FlatList component in React Native to display a list of books fetched from a database, utilizing the useBooks hook and styling the list with themed components.

Full Transcript

All right then, gang. So now we fetched all the user book records and we're storing those in some states. In this lesson, I want to access that book state from the books page in the dashboard group and then output all of those books in some kind of list. So currently on that page, the only thing we have is a view and then some text inside it for a title. And below this title is where we're going to list all the books. And we'll be doing that using a native component called flat list. Before we do that though, I just want to bring in the book state from the books context. And to do that, we're going to need to use the use books hook. So, let me come to the top of the file first of all, and I'm just going to paste in the import for that right here. Now, I'm also going to paste in a few more things we'll be needing for this page as well. So, let me just grab those from the course files and add them in. So, we've got the colors object, which is so we can use the primary color from that palette. And then down here we're adding the themed card component as well which we created back near the start of the series. So we'll use this card component probably for each book in the list that we output. All right. So now we can grab the books in this component by saying const then curly braces and then we want to dstructure the books value. Then we can set this equal to the use books hook which we just imported and invoke it. All right. So now we have that book state which we know should be an array of books or an empty array if there's no books in the collection yet made by this user. So now we can start to list the books out on the page. And one of the easiest ways to do that is by using the built-in flat list component from React Native. So let us first of all import that component up here at the top of the file. And then back down in the template, I'm going to add first of all another spacer component to just give this list a little bit of breathing room. And then I'm going to add the flat list component itself. Now, this component is going to render a list of templates for us eventually, one for each item in the list of books data, but it does need us to provide a few different values. So the first one is a data prop which is where we can supply the books array so that now it knows to make a list based on this data. The second one is a prop called key extractor and this should be a function which returns a unique key or ID for each item in this book's data. Now, React Native uses that unique key to keep track of each individual item in the list, much like it would do in regular React applications for the web. And these keys must be unique for each and every list item. So, for the value, we pass the function and that takes in the item as an argument. And this item refers to each individual item in the books array. Okay. So inside this function, we just need to return the property on the item which we want React to use for the key. In our case, that could be an ID property, dollar sign ID that is on the item itself, which app automatically attaches for us when we create a record. Next up, we're going to add a prop called content container styles. Okay? And we're going to set that equal to styles.list so that we can add the styles for this later on. And you can think of this flat list by the way as a little bit like maybe a div element or even a ul element which wraps all the list items inside it. And these container styles which we apply are the styles which we give that content element. All right. So then finally we need another prop called render item. Now this render item value should be a function which returns a template to be rendered for each item in a list. And inside that function, we automatically get access to an argument from which we can destructure the individual item. So basically this function runs for each item in the books array. And each time it runs, we get access to the individual book item, which is what this thing is right here. So from this function, we can just return a template inside parenthesis and then we can output the different property values from each book inside the template like the title and the author. Now, as a root element to this template, I'm going to use the pressable component, which also needs to be imported from the React Native library. And the reason I'm doing this is because later on, we're going to be able to press on these book items to read more about them. It's going to go to a different page. All right. So, inside that pressable component, we're going to use the themed card component so that each book gets rendered as a card on the page. We'll also pass a style prop to it as well, which is going to be equal to styles.card. And we're going to add those styles at the bottom later on. But inside the card, I want to output two bits of data. The title of the book and also the author of the book. And for each of those things, we're going to use the themed text component. Okay. So, let's do the first one for the title of the book. And for this, we're just going to pass a style prop as well, which is equal to styles.title. title just so we can style it later on. And inside the component, we're going to output the item.title value, which is the title property on the book item we have access to. Now, now underneath that, I'm going to use the themed text component again. And this time inside it, I'm going to say written by first of all, then we'll do the curly braces. And then this time, it's going to be item.author to output the author. And then that's pretty much it for the template of each book. Okay. So finally, I just want to come down here and paste in a few styles for the things we just added. So this list one right here, remember, was the value of the content container style prop. So we're just giving that a bit of padding. I think we do. Oh, margin top, sorry. Then we have a class for the card. So this right here. And in there we give it a width of 90% some margin in the horizontal direction vertical margin some padding uh padding left which is a little bit more and a border left which is the primary color you know from the colors object that we imported up here. So that's like the purple color that we gave to a button and then the border left width as well. For the title we have a font size of 20, font weight of bold and margin bottom of 10. So that was for the book title right here where we say styles.title. Okay. So let's save this now and we'll see in a moment if this works. Okay. So we see just the first one. But what I'm going to do is restart the app so we can fetch the up-to-date data because I actually added a few more. So let me go to the profile page then to books. And yeah, now we can see four books on that home screen or rather that book screen. Awesome. And also just to quickly demonstrate something, I just added a few more books just to show you that this actually auto scrolls as well. So when we use the flat list, it automatically scrolls through the data for us without having to put it inside some kind of scrollable component, which is pretty nice.

Original Description

In this complete React Native tutorial, you'll learn how to develop native apps from the ground up, using React Native and Expo. You'll learn about native components, routing, navigation, styling, authentication and a lot more too. 🔥🥷🏼 Get instant access to ALL premium courses on NetNinja.dev: https://netninja.dev/ 🔥🥷🏼 Get instant access to This Course on NetNinja.dev: https://netninja.dev/p/complete-react-native 🔗👇 Sign up to Appwrite & Get $50 Free Credit: https://apwr.dev/netninja050 📂🥷🏼 Access the course files on GitHub: https://github.com/iamshaunjp/Complete-React-Native-Tutorial 🧠🥷🏼 React Course: https://netninja.dev/p/build-websites-with-react-firebase 🧠🥷🏼 React Context and Hooks Course: https://www.youtube.com/watch?v=6RhOzQciVwI&list=PL4cUxeGkcC9hNokByJilPg5g9m2APUePI 🔗👇 Install Node.js: https://nodejs.org/en 🔗👇 React Native Docs: https://reactnative.dev/docs/getting-started 🔗👇 Expo Docs: https://docs.expo.dev/ 🔗👇 Appwrite docs: https://appwrite.io/docs
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

This tutorial teaches how to use the FlatList component in React Native to display a list of books, including fetching data, styling the list, and handling scrolling.

Key Takeaways
  1. Import the useBooks hook and themed components
  2. Fetch the book data using the useBooks hook
  3. Create a FlatList component and pass the book data as a prop
  4. Define a key extractor function to uniquely identify each book
  5. Define a render item function to display each book
  6. Style the list using themed components
💡 The FlatList component in React Native automatically handles scrolling and can be used to display large datasets.

Related Reads

📰
Behind a Single "Paste" Button: The Tale of Two Completely Different APIs
Learn how to implement a 'paste image' feature in an upload component using two different APIs
Dev.to · Image2
📰
The Django — Snowflake couple… a therapy session
Learn how to integrate Django with Snowflake for efficient database management and explore the benefits of this combination for web application development
Medium · Python
📰
Port Numbers, In Order: Why the List Has Gaps, and the Best Stories Behind the Numbers
Explore the stories behind TCP/UDP port numbers and why the list has gaps, learning about the history and usage of key ports
Dev.to · Yuuki Yamashita
📰
Day 97 of Learning MERN Stack
Learn how to apply MERN stack skills in 100 days and boost backend and frontend engineering skills
Dev.to · Ali Hamza
Up next
Indian Express Editorial Analysis by Chandan Sharma - 1 JULY 2026 | UPSC Current Affairs 2026
StudyIQ IAS
Watch →