Complete React Native Tutorial #5 - Light and Dark Themes
Key Takeaways
This video tutorial demonstrates how to implement light and dark themes in a React Native app using the useColorScheme hook, Expo, and conditional styling.
Full Transcript
All right then, gang. We're making some good progress and we've learned about making pages, a few native components that we can use for templates. We've seen how to navigate between different pages and also how to use a layout file and the stat component as well. So, we've covered quite a lot of ground already. In this lesson, I want to introduce you to the concept of having a light and a dark theme for the application, which kicks in based on your device settings. So, for example, on an iPhone, you can manually set the device to either a light or a dark theme. And then inside this React Native app that we're building, we can access that setting to determine a user's preference and then use it to show either a light or dark theme for the application, too. Now, if you take a look inside the app.json JSON file, you can actually see an option called user interface style where we can override the device setting for the sake of developing and testing the application. So right now it's set to light, but we could change this to be dark and then we'd be able to access that value within the app and conditionally style things differently. Now if we changed the value right now and previewed the app, then nothing would change in the app because we're not actually using that value anywhere. So visually everything's still the same. But that's what we're going to do now by using a special hook that React Native gives to us called use color scheme. So let's go back to the layout file and style the page headers using that hook. And we're going to use the hook inside the component above where we return the template by saying const. I'm going to call this color scheme. We set that equal to use color scheme. And if you click on the option right here which pops up, then it should import that hook for you from React Native. Okay, so this hook should return either light or dark. And in some occasions, it could also return a null value, but generally speaking, it should either be the string light or dark. Now, I'm actually just going to log this value to the console right now inside this layout so that we can see what the value currently is. And when we do that and open up the terminal, I'm going to see that the current value is light. Now, if you open up the app.json file and change the user interface value to dark and save it, we're not automatically going to see that new value in the console. But if we refresh the app by shaking the phone and selecting reload, then eventually we should see that new dark value in the terminal. Okay. So now we're accessing the device mode or color scheme if you like. And now we can use it to conditionally style the app into a light or dark mode. So before we start doing that, I actually want to make a new file to store a bunch of different theme colors in, which we can then just reference from the layout file and also from any other components we make in the future that need styling using those colors. Now to do this, I'm going to make a new folder called constants, which is a folder I like to make to store any global app constants inside of. And in that folder, I'm going to make a new file called colors.js js to store all of the different theme colors in. Now, instead of me writing out all of these from scratch, I'm just going to copy them from the course files and paste them in right here. And as you can see, we have just a single object called colors, which we export. And inside the object, we've got a light property, which is an object that contains a bunch of different colors for the light theme, for all these different things. And we've also got a dark property with the same structure, but different colors, which we'll use for the dark theme. Now, the property names inside these two themes are not important. They're just names I came up with, which I thought were pretty descriptive, but you could have whatever properties and values that you want inside these things. We've also got two other properties up here as well called primary, which is going to be a primary color for UI elements in the app like buttons. That's a purple color. And also the warning color, which is like a pinky red for any errors that we display. And both of these two colors are going to be used for light and dark themes together. they're not going to change, which is why they're up here on their own outside the light and dark objects. Okay, so now we have this colors object and we can import it into any component that we want to style using those theme colors. So let's do that now inside the layout file. Okay, so let me first of all paste in the colors import up here so we can use it. And that comes from one level up, which is why we have the double dots, then into the constants folder and then colors. And now I want to select the current theme from the colors object down here by saying const theme is equal to colors which we just imported. Then square brackets and then the value of the color scheme. So remember that should be either light or dark. Right? If it's light then we're selecting the light property from the colors object which contains all the light colors. And if it's dark we're selecting that dark property from the colors object and that contains all the dark colors. Now like I said before this value could also be null. So in that case we could use the nullish coalesing operator which is a double question mark to provide a fallback default scheme which we'll say is just colors. And that means if for whatever reason we can't access the device scheme and this hook returns null then we'll use the colors in the light theme as a fallback. Okay. So now this theme constant will reference either the light or the dark property on the colors object. And so therefore on this theme value we have access to all of those different theme color properties within the light and dark objects like the background or the nav background or text color icon color and so forth. So let's try using some of those different theme color properties in the layout file. I'm going to style the header from the stack to have the background color plucked from the theme object. So the value should be theme.nav background. Also we'll update the header tint color as well. And the value of that should be just theme.title. And that is another property we defined. Awesome. Now let's try this out. And yeah, we can see that dark background up here now. So it's using colors from the dark palette. And that's because if we open up uh this app.json file, we have dark right here. Now, if I change this to light and save it, we are going to need to refresh the app. So, let's do that. We'll reload it, but then we should see a different background color up here and a different tint color. And that's because we're now in light mode and we're using the light palette. Awesome. So, that's going to apply to every page that has that navbar thing at the top. So, if we go to these other pages, yeah, we can see it still works. Um, if I change this back to dark just to make sure it works for the other pages as well. Let me refresh this again. And then if we go to about. Yeah, that works. And if we go to contact. Yeah. So, it's applying now across the whole kind of spectrum. All right. So, now what you could do if you wanted to is you could go to different pages and style them the same way. So, let me close a few of these down. And what I'm going to do is go to the about page of here and do something similar to style these for light and dark. So if we go to the layout, what I'm going to do is copy this import because we're going to need this. And I'll paste it over here. And then we're also going to need the use color scheme thing, but we'll import that manually in a second. I'm going to copy these two things so we can access the color scheme like so. And then up here we need to import the use color scheme hook. All right. So now we could use this theme to style things in line. Now we can't access this down here in the stylesheet because we make it within this component, right? And the stylesheet is down here. So we have to do these in line for now. But you saw before we could just add an array here if we wanted to like so to apply additional inline styles as well as this container. So I could add an object right here and I could say that the background color is going to be the theme dot background. Was it background color? Let me have a look inside the colors. It was just background. So theme do background. Take away that color bit. All right. And I could do the same for text. Um I could add another style here which would be theme.ext. Let me save it just to make sure this works. And then we'll go to the contact page. And oh, not the contact, sorry. It's the about page, isn't it? Let's do that. Yeah, we can see that now we've got the dark background. But if we open up the app.json file and change this to light, and then we're going to need to refresh the app. So, reload. And then we'll go back to the about page. We can see now it's the light background. So, it's automatically changing those styles based on the value of this user interface style which a user can set on their phone. That's their preference, right? Okay, cool. Now, you might have actually noticed something as well. If I change this to dark and reload this app, so the header becomes dark. Now, look at these things up here. You see this is dark text and the battery is dark and this internet symbol is dark. Well, we don't really want that, do we? And we kind of want this little bit of status bar up here to auto color itself based on well if we have dark mode or if we have light mode. And we can do that pretty simply. All we have to do is come to the layout and I'm going to wrap this thing in a fragment because we must have one root element when we return a JSX template. So let me wrap this in a fragment because I also want to within this add another one which is going to be a status bar component and that comes from expo status bar over here. So let's import that and then we can just add a prop which is called value and we're going to set that to be auto and then it's going to auto update over here. You can see it's gone light now which is awesome. But then if I change to dark mode, if I come over here, sorry, if I change to light mode again, we're already in dark and save it and then restart the app like so, then you can see it becomes dark on a light background. So that's nice. Okay, so we've seen how to use this use color scheme hook to grab the user scheme setting value and we've used that to select a color palette we can style components with. And this is really cool. But in the case of this about page, it's just looking a little bit messy really. And if you think we'd have to do this for each and every text component or view component or other native component that we might use, then you can imagine it's going to get even messier. So what I like to do instead is make some common basic reusable themed components which conditionally apply these light and dark themes to them. And then we can just drop those into the page components where we need them. And that keeps everything much tidier and easier to manage. So once I've stopped recording, I'm going to remove all of this theme stuff from the about page. And then in the next lesson, we'll make those reusable drop in themed components instead.
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: Prompt Craft
View skill →Related Reads
📰
📰
📰
📰
npm Is Finally Going to Stop Running Strangers’ Code on Your Laptop
Medium · JavaScript
Webhook idempotency: how to handle duplicate deliveries safely
Dev.to · Yuriy
[Rust Guide] 13.7. Using Closures to Capture the Environment With Iterators
Dev.to · SomeB1oody
Behind a Single "Paste" Button: The Tale of Two Completely Different APIs
Dev.to · Image2
🎓
Tutor Explanation
DeepCamp AI