Full React Tutorial #11 - Props
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
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: React
View skill →Related Reads
📰
📰
📰
📰
React Performance Optimizations I Actually Used in Production
Dev.to · Ankita Budhia
Web Development training and Placement in Electronic City — Full Stack (HTML, CSS, JS, React, Node…
Medium · JavaScript
Document Object Model [DOM] CRUD Operations
Dev.to · Madhan Raj
I Found a Surprisingly Fun Way to Practice Frontend Development
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI