Full React Tutorial #11 - Props

Net Ninja · Beginner ·🌐 Frontend Engineering ·5y ago

Key Takeaways

Passes data from parent components to child components using props in React

Full Transcript

all right then gang so right here in the home component we're outputting a list of blogs right and we're doing that directly inside this home component now if you think about it if you're building a real blog website you might have the list of blogs in various places on your website it might be a home page it might also be on a search page or a tag page or a category page there's going to be several different areas which is going to use the same logic essentially where we're cycling through the blogs and outputting a blog preview for each one right so if we were to do that in our project we're not going to do that but if we were to do that then we'd essentially be repeating this code over and over again in different components for different pages eventually so sometimes what i like to do is where i have pieces of components or bits of templates that might be reused in different positions or different places in the website i like to make that bit of template into its own reusable component so for example if i was to make a component called blog list then i could drop that blog list component in any other components in the project so if i was to have a category page later on i could just get that blog list component and drop it in if i had a tag page then i could get that blog list component and drop it into the tag component that makes sense right so we're just reusing the same logic over and over again and we don't have to therefore repeat all of this so that's what we're going to do now we're going to create a blog list component and apart from being reusable as well we're gonna see later on how we can pass in different data into the reusable component every time we use it and we'll do that in the form of props so for example on the home page we might list all of the blogs and show a preview for all of the blogs starting with the latest one but on a search page or a search component we might only show the blogs that match the search term so the data is going to be different the structure is the same but the blogs that we use are going to be different so we can pass in data into these external components as well in the form of props and we'll see those shortly but first of all let's create an external component called blog list to house all of this logic or all of this template so then let's start by creating a new file called blog list.js and then inside here we need a stateless functional component hit tab and we'll call this blog list like so all right so the template is just going to be the template that we have currently right here so let's grab it from here i'm going to cut it and then down here i'm going to first of all create a div with a class of blog hyphen list like so and i'll place it inside here like so so we're still cycling through the blogs and we're still outputting a bit of template for each one so let me save this and now we want to nest this blog list component inside the home component right here so i'm going to say blog list and notice i can click on this and it's going to auto import it for me at the top so let me close this off now and there is a problem with this and we'll see that in a second but let me save it and try and preview it and we get an error and it says blogs is not defined so that's because in the blog list component we're trying to map through the blog so we're using data here but it doesn't know what blogs is because the blogs data is not defined in this component we can't just use any data in another component where it's defined right here because it can't reach that okay so there's a couple of different ways around this the first option is to re-declare all of this data inside this component instead of home so we delete it from here and we place it over here instead the second option is to use props whereby we pass this data from this home component into the blog list components now i'll be using that second option props for three reasons first of all it's going to make our blog list component more reusable and we'll see exactly how later on second it allows me to still use this data in the home component later on if i need it in the future because the data is still going to be declared here and third it allows me to show you how to use props all right so props are a way to pass data from one component a parent component into a child component so this is the parent component and this is the child component right here so we want to pass the blogs data into the blogs list component and the way we do that is by making a property name on this tag so you can call it what you want i'm going to call it blogs since that's what we're passing and this is going to be a dynamic value so curly braces and the value is going to be blocks like so so we just passed that in and now this is being passed into the blog list component as a prop this right here is a prop so we need to receive it inside the blog list components and we get access to an argument inside this function inside the component called props so now this property right here blogs is going to be on this props object so i could access the blogs from it so let me do this i could say const blogs is equal to props dot blogs like so and what i'm gonna do is just log that to the console so i'll say console.log and in fact we'll log out the props as a whole as well and blogs all right so let's come over here and inspect this notice first of all if i refresh everything's still working because we're now passing the blogs into this component and we're grabbing the blogs right here storing them in this constant and then we're cycling through that constant at the bottom so this works now as it should but let's go over here and see what's in the console so this top one right here this is the props object and you can see it has a blogs property on it which is an array and down here this is the blogs themselves all right so any props that we send through into a component are attached to this object which we automatically get as an argument in the component and we can access them now we can pass multiple props if we want to so i could pass in another prop for example called title like so and set that equal to some kind of string now it can be a dynamic value which is stored in some kind of variable but i'm just going to pass in a string value so the title is going to be all blogs and then what i could do is access that as well from the props i could say const title is equal to props dot title all right so let me delete this console.log we don't need that anymore now what i'd like to do is output this title above the blogs so i'll do an h2 and then inside there we'll do the title so if i save this now and preview we can see we get the title as well and if we change that just add an exclamation mark then it's going to update so then that's how we can make a component to take in props data and then use that data inside that component now i said before it makes this component now this blog list component more reusable and it does we can now use this blog list component anywhere in our application whether that's in the home components or in a different page component later on and i'm going to demonstrate that in the next tutorial but before we finish this video i want to show you one more thing if we go to the blog list right here we can see what i accept in this props object and then we're grabbing the different properties from that props object right here and storing them in these variables now an easier way to do this is to destructure the props directly inside these parentheses so i could comment these things out and i could get rid of this and i could destructure from the props directly by saying what properties i want from them so i could say i want the blogs and also the title and now we have these two properties available to us inside this component so this should still work i can delete this entirely save it and check this out it's still gonna work if i refresh

Original Description

Hey gang, in this React tutorial we'll learn about props - which is a way to pass data from parent components into child components. 🐱‍💻 🐱‍💻 Course Files: + https://github.com/iamshaunjp/Complete-React-Tutorial 🐱‍👤🐱‍👤 JOIN THE GANG - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join 🐱‍💻 🐱‍💻 My Udemy Courses: + Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript + Vue JS & Firebase - https://www.thenetninja.co.uk/udemy/vue-and-firebase + D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 🐱‍💻 Helpful Links: + HTML & CSS Course - https://www.youtube.com/watch?v=hu-q2zYwEYs&list=PL4cUxeGkcC9ivBf_eKCPIAYXWzLlPAm6G + Modern JavaScript course - https://www.youtube.com/watch?v=iWOYAxlnaww&list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc + Get VS Code - https://code.visualstudio.com/ 🐱‍💻 🐱‍💻 Social Links: Facebook - https://www.facebook.com/thenetninjauk Twitter - https://twitter.com/thenetninjauk Instagram - https://www.instagram.com/thenetninja/
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

Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →