Complete React Native Tutorial #20 - Activity Indicators

Net Ninja · Beginner ·🔧 Backend Engineering ·1y ago

Key Takeaways

Creating a custom loading screen with React Native

Full Transcript

All right, so we made a component in the last lesson called use only which we used to protect certain pages from unauthenticated users. Like all these pages right here in the dashboard group. And we did that by wrapping the layout within that group with the use only component. And we also did the opposite. We made a guest only component which wrapped the auth pages so that only users not logged in can view those pages. Now inside those two components that we made, we also output a little bit of loading text as the content to show while we kind of figured out if the user was logged in or not. And while we ran any redirect logic. And that loading text ends up showing on a blank unstyled white screen for just a second and it looks horrible. In fact, I'm going to show you this now. I'm currently on the profile page logged in. Now when I log out, we're going to see for just a split second that white screen with a bit of text in the top left. And it doesn't look nice at all, right? So in this lesson, we're going to make a better loading screen that we can show that fits in with the color scheme of the user's phone as well, light or dark. So to do this, we'll be making use of a native component called activity indicator which shows like a little spinner. And we'll be embedding that into a custom themed loader component that we make and that we can then use instead of this text right here. So before we make that themed loader component, let's see what this activity indicator looks like out of the box. To do that, I'm going to head to the login page and we're just going to output the activity indicator at the bottom down here somewhere. So then, let me come underneath this link at the bottom and I'm just going to start typing this component which is a native component comes to us from React Native. And it's called activity indicator. So if we click on this one, it should import it from React Native up here. Yep. And now if we go down here, we can customize this using a couple of different props. So let's close it off first of all. And the first prop is going to be the size and we could set that to be for example large or small. So I'll say large. And then the next one is the color and I'm going to set that to be just white so it shows up. All right, so if I save this now and look down here, we can see that activity indicator which is like a little loading ticker, right? Now this is good, but if I'm on the light mode, so let me come down here and let me change this to light. If I save it, I'm going to have to reload the app. So let me shake that reload. And then go to the login page. It's white so it doesn't really show properly, right? So instead of just using this standard activity indicator and giving it a hard color, what I'd like to do is make that component like I said before which is some kind of themed loader. Now it's going to use this under the hood, but inside there we can tap into the user's color scheme and we can dynamically apply a different color to this activity indicator then. Okay, so now let's go to the components folder and create this new file called themed loader .jsx. All right, so inside here then, I'm going to import a few things and I'm just going to paste those in. So it is the activity indicator component we just used on the login screen. In fact, let's get rid of these things over here, give us some room. And then also the use color scheme hook as well which will need to grab the user's color scheme. On top of that, we have the colors object being imported so we can use those colors as well. All right, so then down here, let's make the component by saying const themed loader is equal to a function and inside here, we need to first of all grab the color scheme. So we'll say const color scheme is equal to use color scheme. We've seen all this stuff before, right? In all the other theme components. And then we need to actually use that value to get a theme from the colors object. So we'll say const theme is equal to colors and then in square brackets, we're going to pass in the color scheme which remember is either going to be light or dark. So it's going to be either the dark property or the light property that we get access to from that colors object then. But it could sometimes be null and we need a fallback. So we'll say double question mark and the fallback is colors .light so the light theme. All right, so now we need to actually return a template. So let's do that. Return and inside this template, we want the activity indicator. Now for the size we'll say it's going to be large. I mean, you could accept this as a prop here as well if you wanted to and the user could then choose a large themed loader or a small one. I'm just going to keep it large for all of them. And then for the color, this is the dynamic part. It's going to be a theme .dots And what should we use? What color? Let's have a look at the colors object inside the constants folder. I think the text color. Because then it's going to be bright for the dark theme and it's going to be dark for the light theme, right? So let's use the text color. So theme.text like so. All right, so now what I'm going to do is come to the login page over here and instead of using the activity indicator, I'm going to use the themed loader which we will need to import up here. So let's do that as well. Duplicate that and we'll change this and this to themed loader. All right. And then we can also get rid of this one up here as well and the text import. And the pressable. Great. All right. So if I save this, hopefully we'll see the themed version of this now instead. So let's do that. And okay, an error. Let's have a look what that is. We'll scroll right down and then come back up a bit. Okay, so it says check the render method of login. Element type is invalid, expected a string. Okay, and it turns out I've just not exported it. So let's do that. Let's export default themed loader like so. Save this and then if we go to the login page now, yeah, there we see it. Awesome. So that looks good. And now if we go to the app.json file and change this to the dark mode again, hopefully the loader will be light. Currently it's dark, right? For the light theme. If I change this and then reload the app and then go to the login page. Yep, now the loader is light. So this is all working. This is good. But I want to do one more thing because this themed loader is actually going to be a loading page that I want to show instead of that current text that we're getting in the top left. If we open up those components, where are they? Use only for example. This thing right here. It's basically just a text component on a white background. Now I don't just want to show that little loading component up here in the top left instead. No, I want to show a whole kind of view which stretches across the whole screen with the activity indicator in the middle. So what I'm going to do then is wrap this with a themed view. So I will say themed view right here. Click on this to import it. And then I'm going to grab this and I'm going to paste it down here. And we nest the activity indicator in the middle. Now I also want to apply some styles to this. So we'll say style is equal to an object. We'll just do it in line and this is going to say flex is one. Then justify content to the center. And then finally, we want to align items to the center as well. And this is also the first of all with flex one, the view takes up the whole height of the screen. Justify content to bring the loader down into the middle vertically. Align items to do it horizontally as well. So we're going to get a big screen which uses the themed view so the same background color as whatever the theme is. And then in the middle, we're going to get that loader. All right, so if I save this now then over here, you can see it's taking up all the available room. It's pushed everything else up to the top. But we don't want to use it there on the login page anyway. So let's get rid of it and remove it from the imports as well. And we want to use it inside this thing right here so we can use the themed loader there instead. Click on that to import it. And then we'll get rid of the text import. And the same for the guest only one. So let's change this to be a themed loader and we can get rid of the text import like so. All right, so now what I'm going to do is I'm going to try logging into an account. So I think one of them was Bowser and that was at ninja .dev, right? I think. And then test 1 2 3 4. Login. All right, yeah. So now I'm logged in. Now before when we logged out, we saw that little white page. Now you're not going to see it for long, but just try and pay attention. When I click on log out in a second, you're going to see that loading screen instead, that themed one. So log out and you saw it very briefly, right? It just looks a little nicer. Now if the internet connection was bad and that request took a little bit longer, then you would see it for longer and that would be a better user experience I think showing that than just a little loading message in the top left. So then now we've created that themed loader and I think in the next lesson it's time to move on and we're going to set up the database so we can start storing book records.

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

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 →