Complete React Native Tutorial #28 - Fetching Single Records

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

Key Takeaways

This video tutorial demonstrates how to fetch a single record in a React Native application using Expo, by creating a function to fetch the record by ID and then using the use effect hook to run the function when the component loads and when the ID value changes.

Full Transcript

Okay then my friends. So we made a dynamic route for the book details in the last lesson but at the moment all we do is show the book ID, right? And instead we want to show the full book details, the author, the title and also the description. Eventually as well we're going to be adding a delete button on this page so that a user can click on that to delete the book. Anyway, there's a few ways we can show the book details, but I've decided that we're going to manually fetch the individual book from this page. And I like this approach because then if we deep link to a details page from somewhere else and it's going to try and fetch that book when we land on this page. Whereas if we try to find it from the existing book state then well that wouldn't work using the deep link because the state would be empty when we first open the app. Anyway to do this we first of all need to flesh out the function inside the books context file to fetch that individual record. And the function is this one right here fetch book by ID. And we already accept an ID into the function as an argument which we're going to use now to fetch the book. So inside this try block we're going to say const response is equal to await and then we want the databases instance that we had and then we use a method on that called get document. So this is how we fetch a single record. And inside here we still need to pass in the database ID and the collection ID like we do down here. So let's paste those up here as well. That tells us that we want to go into this collection to grab a document. And then we need to pass in the ID of the book that we want to fetch, which remember we have access to right here because we pass it in as an argument. So let's just output that. And then down here, we're not going to update any state because this is a full list of books, right? We don't want to update it with a single book. Instead, what we're going to do is just return from this function. So we'll return the response which will be that book record. Okay. So now that's done, we can head back to the details page and invoke this function. The first thing I'm going to do inside this file is just paste in a bunch of different imports that we're going to be needing as we flesh this out. Then I'm going to come to the component and I'm going to make some state for the book. To do that, we'll say const. Then in square brackets, we'll call this bit of state book. And then we need a function to update it, which we'll call set book. And then we set that equal to use state which needs to be imported remember from react and we invoke that. All right. So the starting value of this is going to be null because we've not fetched the book yet. All right. So now we'll use the use effect hook to run when the component first loads and also when the ID value changes. So we can say use effect and also make sure that gets imported from React. And then inside this hook we can pass a function as the first argument. Now, as a second argument, we're going to pass that dependency array, which just contains the ID. So, when the ID changes, it's going to rerun this function to refetch a different book. Okay. So, inside this function then is where we need to fetch the book. So, up in the component, we need to grab the function we need from the book's context, don't we, to do this. So, I'm going to say const curly braces, and we want the fetch book by ID function. All right? and we set that equal to use books and we invoke it. Right? So now we can use this function down here in the use effect hook to actually fetch the book we need. Now the way we're going to do this is by actually making another function which is async right here. And I'm going to call this function load book. Inside the function I'm going to say const book data is equal to then await. And then we're going to call that function fetchbook by ID. And we'll pass in the ID as an argument. Remember that ID comes from the wrap parameter which we grab up here using the use local search params hook. We did that in the last lesson. Anyway, once we have that book data, which is just a book object, we can update the state by saying set state and then passing in the book data. Then we just need to invoke this function down here so that it gets run when the page first loads. All right. Okay. So cool. Now when we land on this page, we grab the ID from the route and then we run the use effect function which gets that book from app. After that we update the state to be that book object. So now we just need to output that book on the page. So I'm not going to waste your time by writing out all of this template from scratch. It's going to be simple and I'm just going to paste it in for all my course files. But let's quickly go through it. So we have a theme text component for the book title. So we're using that book states and then accessing the title property on it. Same for the author down here, then a spacer, and then we have themed text with a title prop set to true to say the book description. Below that, another spacer with a specific height. And then finally, we output the book description inside themed text. All right. And actually, I think what we need to do is wrap that with a themed card. So, let me just grab all this and delete it and paste it in again. And this time, we've got the themed card. All right. So, that wraps all of these. Okay. So, when I save this, it's probably not going to work, but let's just try this anyway. I'm going to save it and then go to a book. Yeah. And we get an error. Now, the reason this doesn't work is because when we first load the app and come to a details page, the book state is null to begin with until we fetch that book from the database. And so, React is trying to render the page template with all that book data when the book state is null. And that's why we get the error. So, to fix this, we can just add a little if check above where we return the template. And inside this check, we can see if we don't yet have a value. So if not book, if that's true, it means we don't have the book yet. It's still null. And in that case, we can return a loading template instead of this one down here. So let me return inside here. And then for the template, I'm just going to paste in a snippet from the course files. And that is a themed view using the safe area with these container styles. And inside that, we render the loader component that we made earlier in the course. All right. Okay, then. So now when we load this page, if the book's null to begin with, we'll show the loader. Then when the book does load, it rerenders and it shows the book details instead. So we're going to try this in a second, but I thought first of all, I'm just going to paste in a few styles as well to make sure that it looks okay on the screen. So we have one for the title, and that's for this thing over here, the book title, and also for the card itself, just to give it some margin. Going to save this, and then I'm going to head to the profile page. Let's cross our fingers. Click on a book and okay themed loader doesn't exist. Of course it doesn't because we need to import this. So where does that come from? Let's do that down here. We'll change themed card to themed loader and do the same here. Save it again and go to the profile books. Click on one of these. And yeah, that works now. Awesome. So let's try a different one. Brill. So that's now working.

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 fetch a single record in a React Native application and display the details on a book details page. It covers creating a function to fetch the record by ID, using the use effect hook to manage state, and handling loading states.

Key Takeaways
  1. Create a function to fetch a single record by ID
  2. Use the use effect hook to run the function when the component loads and when the ID value changes
  3. Manage state using the use state hook
  4. Handle loading states by displaying a loader component
💡 Using the use effect hook to manage state and fetch data when the component loads and when the ID value changes is a key concept in React Native development.

Related Reads

Up next
Indian Express Editorial Analysis by Chandan Sharma - 1 JULY 2026 | UPSC Current Affairs 2026
StudyIQ IAS
Watch →