React Native Tutorial #22 - Passing Data Between Screens

Net Ninja · Beginner ·🛠️ AI Tools & Apps ·6y ago

Key Takeaways

Passes data between screens using React Navigation in React Native

Full Transcript

okay then gang so we've seen now how to navigate between two different screens and in this video what I'd like to do is actually start to pass data between that navigation so from the home screen to the review detail screen now the first thing I've already done is I've got rid of the button and the on press Handler up here which we just used in the last video to take us to the other screen the review detail screen and I've also deleted the button and the unpress handle up from this as well because we're not going to use those buttons anymore this time what we're going to do is use some data which I've already pasted in as a bit of state and I've already imported you stay up here and we're going to cycle through that data output a list and then we're going to attach some kind of on press handler to each item in that list when we click on that it passes that review data through to the review details screen okay so this data right here Const reviews and set reviews is equal to use state which is an array of objects each object is a single review and each object has a title prop which is the review title a rating which is an integer a body which is the review details not much in it but that doesn't really matter and also a key which is a string remember when we use a flat list it's automatically going to look for that key property so that's what we are going to do now a flat list now we have our view which has this style of container and inside that I'm actually going to delete this text because we don't want that home screen actually to be there for real so I'm going to delete that and now create a flat list instead so we need to import a few things first of all I'm gonna get rid of this button we don't need that anymore and we do need flat list we're also going to be using touchable opacity in this as well so that we can click on each item so let me import that switchable opacity and I think that's it okay so let's go down here and do this flat list so flat list like so and remember we need a couple of different props on this flat list the first one is the data prop which specifies what data we want to cycle through and that's going to be this thing right here reviews so let's copy that dude and paste it in and then the next thing is going to be the render item function so that is going to be equal now to a function and that function is going to return some JSX now instead of these curly braces let's just put normal parentheses because at the end of the day we're just returning something here on the same line and the JSX is going to go in between those now remember we have access to the item by D structuring here so we can grab that by using curly braces then item so now every time we output something we're going to have access to the individual review via this item right here okay now inside this we want to return first of all touchable opacity component and then we're going to embed a text component inside that so the text first of all is going to say item title so we're basically taking the current item and grabbing the title property yeah and then this touchable opacity and by the way let's add some styles to this text I'm going to say style is equal to global styles and then on the global styles remember we had a title text property so we're going to use that for this now the touchable opacity right here we need to add an on press handler to that so this on press handler I'm just going to write the function inline will be an anonymous error function and inside this error function what do we want to do well we want to use this navigation object to navigate to the review details so let's say navigation dots navigate like so and then remember we pass a string in here to say where we want to navigate to that is going to be review details like so now we also want to pass some data through and what we can do is pass a second parameter into this navigate method and that second parameter is going to be an object and this object represents the different parameters or data that we to send to do review details now I could add any number of key value pairs so for example I could send a name property through and say Shawn I could also send like an age property through which could be 23 as many as I want they're all contained inside this object which represents the parameters of data we're sending to the review details page now we already have an object that we want to send to the next page and that is this thing right here item because remember the item refers to this entire object for every iteration so for every iteration we can send the item through so that when we click on the item we then go to the review detail screen and we have access to that particular item data and it's going to have all of these properties on it title rating body and key okay so that's all that done and if we save this let's just make sure we get no errors first of all over here and we don't we see these different things and if we click on one of them then it does go through to the review detail screen and we're sending that data through this item but we're not doing anything with it in this screen yep so we need a way now to accept that data and do something with it so let's now go to the review detail screen and down here we can output whatever data that we get back so instead of just outputting this we can output for example the title or the body or the rating now the way we do that is by using this navigation property up here that we get from the props so let me output some data we're going to say navigation then we use a method called get per am okay now this get per am method gets us a particular parameter from this object remember this object has different keys and values in it so we want to get one of these different parameters the title the rating the body etc so say for example we want to get the title we just say we want the title from that object and now it's going to grab that and it's going to output it right here in this text widget so if I save this now well let's have a look if this works so let's click on this one got to catch more again and we see it right there if we click on a different one then we can see it there so these all work right now instead of just showing the title let's show everything so I'm going to duplicate this and replace the title right here with body because we have a body property on this data right here and we also have a rating so let's output the rating as well so let's save that now let's see if this all works so let's click on one sell them and we can see all of the details right there click on another and all of the details are there and this one and all of the details of their awesome so this is all working now so that's how we send data when we're navigating from one screen to another we use a second argument and that argument is going to be an object the object should have different key value pairs in it and each key and value pair is a particular parameter we then grab those parameters by using the navigation object that we get from the props and we use the get parameter to get a specific parameter from that object okay simple enough now I know the design of this just not great yet but we are going to come back to that later for now what I'd like to do is customize the header options a little bit and we'll see that in the very next tutorial

Original Description

Hey gang, in this React Native tutorial we'll see how to pass data between screens when navigating from one to another (in our case from Home to Review Details). ---------------------------------------- 🐱‍💻 🐱‍💻 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

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →