React Native Tutorial #21 - Navigating Around

Net Ninja · Beginner ·🌐 Frontend Engineering ·6y ago
Skills: React90%

Key Takeaways

Builds a navigation system using React Native's Stack Navigator to navigate between screens

Full Transcript

our other gang so at the minute we've created this Homestuck navigator right here and registered these two different screens and then when we surround it with this create app container function it returns a component which we're rendering right here and this determines which screens currently show currently this one right here because it's at the top of this configuration but that's all its really doing at the minute we need a way now to navigate between these two different screens using this navigator so how exactly do we do that well first of all let's go to the home screen because that's the one that loads up automatically by default and what I'd like to do is place a button in this component and when we click on that button take us to the review details component over here so first things first let's import button up here and then let's create that button down here and it's self closing we need a title prop on this button which is going to say something like go to review debts okay so if I save this at the minute we should see that button over here but when we click it it's not really gonna do anything yet so if I click it nothing happens so we need an on press handle so what I'm going to do is add that right here on press and set that equal to some kind of function which I'm going to define up here so Const and we'll call this press Handler and set it equal to an arrow function now inside this arrow function what we want to do is then navigate to the review details screen now how exactly do we do that well when we configure our navigation step like this every screen that we configure automatically gets a navigation property on the props assigned to it so in here you know that we can take in props right well we get a property on the props called navigation so props navigation like that and we can use that then to navigate to different screens in the stack so I'm going to get rid of that because what I like to do instead taking the props directly is 2d structure what we need from the props and we're going to D structure navigation so now we have access to this navigation object right here and what we can do is say now navigation and we can use a method on that call navigate and in here we can pass a string and the string is going to be where we want to navigate to and it has to be one of these things the names of the different screens so either review details or home so not this name and not this name even though they're the same whatever you called it right here so we want to go to review details so let me copy that dude and paste it in here in a string that's where we want to navigate to so then react navigation is going to look at this and it's gonna find this property inside our screens object right here and it's gonna say okay well if you want to navigate there I'm gonna show this screen this component right here that we import it and it's gonna then take us to that and show it on the screen so let me save this now and see if this works I'm gonna whoops no it doesn't so this there's a problem with oh that's because we've not said right here what we wants happen on press so we want to run the press handle up function like so so let me save that now and hopefully how this will work okay so when we click on this now go to review details and we get that screen and take a look at this we have this arrow automatically in the header we did not have to do a thing to put that there react navigation and the stack navigates in particular automatically places that arrow there and it knows that we've come from another screen and if we press this it can take us back to that original screen so if we think back to our stack we had the home screen at the bottom then we clicked on the button and that added this screen to the stack on top now if we click on this arrow right here it's gonna pop this screen back off the stack and it's gonna show the home screen underneath so click on that and we can now see the home screen underneath awesome so that works now that's an easy way to navigate from one screen to another we can also use a diff method and that is the push method so let me say navigation dots push and then if we play seeing it review details this is going to do the same thing only this is a bit more explicit right so remember when we were talking about the stack to add something to the stack we push it on and to take it away we pop it off so this is a more explicit method right here but it's going to do exactly the same thing so let's save that and just test it out and again click on that button and it still works and if I click on the back button it still pops it off automatically now when we're using these two there's not really much difference in the two different methods and I'm going to stick with this one at the top navigate but when you're making more complex apps sometimes you need a little more granular control over the stack and how it works and then you can use this push method for that now just like we have a push method we have a pop method as well and remember that pops a screen off the stack and that's actually what's happening behind the scenes when we click on this arrow but we can manually do that as well so let me comment out this one and bring this one back first of all then I'm gonna go to the review details component over here and the same in this component I want to add a button now inside here that when we click it is going to pop that screen off the stack so it's going to do exactly the same thing as the arrow at the top but this is a more manual approach and sometimes you might want a button or a link or something that when a user presses on it takes you back so I'm going to show you how to do that so first things first let's import the button at the top and then down here will create that button and the title this time is going to be something like back to home screen so title is equal to back to home screen like so and then we also need an on press Handler so let's create a function up here to do that I'm going to say Const and then press handle again equal to a function and inside that function all we want to do is use navigation and remember we have to grab that the props so we destructor it navigation and again the reason we get access to this is because we've used this particular component in the stack navigator right so every component that we use in the stack navigator or automatically and gets this if we were to go to the about component we wouldn't get that because we've not configured that inside the stack navigator just the ones we've configured here and get access to it so we take that and we can use the go back method like so and that now pops a screen off the stack so now if I save this in fact let's not make the same mistake twice let's put press Handler in here then save it and see if this works so go to review details we can click on the back button that works let's do it again and this time click on this button and it does exactly the same thing it pops a screen off the stack and goes back to the home screen so now we know a little bit about how to navigate between screens let's add some data to the mix and see how it can pass data from one screen to another as well so basically we want a list of different reviews here eventually and when we click on a review we want to go to this screen and navigate here but we want to pass that particular review data into the component so we can render it so we'll see exactly how to do that in the next video

Original Description

Hey gang, in this React Native tutorial we'll see how we can use our stack navigator to navigate around our app from screen to screen. ---------------------------------------- 🐱‍💻 🐱‍💻 Course Links: Course files - https://github.com/iamshaunjp/react-native-tutorial 🐱‍💻 🐱‍💻 Other Related Courses: + Complete React Tutorial - https://www.youtube.com/watch?v=OxIDLw0M-m0&list=PL4cUxeGkcC9ij8CfkAY2RAGb-tmkNwQHG + React Hooks & Context Tutorial - https://www.youtube.com/watch?v=6RhOzQciVwI&list=PL4cUxeGkcC9hNokByJilPg5g9m2APUePI + Modern JavaScript Tutorial - https://www.youtube.com/watch?v=iWOYAxlnaww&list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc
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 AI Lessons

Had my Frontend Developer interview with Capgemini (Application Developer) today, and I wanted to…
Prepare for a frontend developer interview with Capgemini by reviewing JavaScript fundamentals and practicing common interview questions
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with 10 essential tools for modern web app development
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with top 10 developer tools in 2026
Medium · JavaScript
The US Frontend Engineer Market in 2026: A Data-Driven Reality Check (and the Bias That Stops Us Seeing It)
US frontend engineer hiring demand peaked in 2022 and remains flat-depressed in 2026, contrary to common assumptions
Dev.to AI
Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →