Complete React Tutorial (& Redux) #12 - Nesting Components
Key Takeaways
Demonstrates how to create and nest multiple components in React
Full Transcript
all right there so we have our react app up and running but the minute we're only showing this one component this is the only component being rendered to the Dom and that is the app.js file right here this component and this is known as a root component in react and a root component is the mother of all components that sits at the top of our application structure so typically react apps have one root component which is always rendered to the Dom and in our case that is app KS here it sits at the top of the tree now every time someone visits our application this root component is rendered then if we want to show different components then we can nest them inside that root component and we could switch these components in and out dependent on the user's behavior on the website now when a component sits inside another component like that we say that that component is nested in it the navbar component is nested inside the app component so let's have a look in the code exactly how we can nest a component all right then so what I'd like to do is create a new component and then nest that component inside this routes app component right here so first of all let's create a file for that new component in source I'm going to call this ninja's dot j s and we want to create our new component in this file but first of all let's just have a look at how this component was created very similar to how we created them in the first few lessons we're saying here class up extends component now before when we created them we said react component when we just added the react library into the HTML file but we don't need to do that anymore because what we're doing at the top here is we're importing react but also component from the react library as well so we can just say class up extends component because of that import and that's going to do exactly the same thing so first of all let's copy this import right here whenever we create a class-based component we're going to need to import these two things react and component all right so then down here let us create that class-based component I'm going to call this ninjas and it's extends components like so okay so inside we need that render method and that render method returns a JSX template so I'll do a div first of all and give this a class name of ninja and let's do that again okay that works and inside another series of divs one for the name one for the age and one for the belt so first of all a div for the name the name is going to be Ryu and then after that we'll do another div this one for the age so age is going to be thirty and then finally one for the belt colors of div and then belt is going to be black like so all right so we've created that class now we need to export it because we want to import it into this component and then nest it so at the bottom export default and then ninja's which is the name of that class so now we've created the component and we've exported it at the bottom so what we could do is import that component into this file and then we can nest that component inside this template so let's import it first of all it's called ninjas so we'll import ninjas from and then in quotations its dot forward slash that means this current directory since but in the same directory right here then the name of the file which is ninjas the import is clever enough to figure that out okay so we've imported that component now we can nest it inside this component but how do we do that what do we do to be just right ninjas like that well no but it's pretty simple we just need ninjas in a tag like so we output it as if it was an HTML element exactly as we write it right here self closing as well okay so now that is going to take the template from this component and it's going to nest it right here like so so if we save that we should see it on at the screen we see that component right here is nested inside the root component and it's being rendered now to the Dom okay cool so that's how we nest I can ponen but why did we need to do that in the first place why did we need a new component to nest why couldn't we have just added all of that code directly here instead well the idea of different components is that we're keeping our code more modular and that component ninjas jeaious it may have its own states and methods at some point which we don't want to muddle up with the states on methods of this component and also if we wanted to we could reuse this component and nestea in different places later on as well so that's how we nest the component now it would be nice to make the content inside this component more dynamic and not just hard-coded like with that so to do that we'll use props and I'll explain those in the very next video
Original Description
Hey gang, in this React tutorial I'll explain exactly how we can create multiple components and nest them within other components.
----------------------------------------
🐱💻 Course Links:
+ VS Code editor - https://code.visualstudio.com/
+ GitHub repository (course files) - https://github.com/iamshaunjp/react-redux-complete-playlist/tree/lesson-12
+ React CDN - https://reactjs.org/docs/cdn-links.html
----------------------------------------
🤑 Donate @ https://www.paypal.me/thenetninja
🎓Find me on Udemy @ https://www.udemy.com/user/47fd83f6-5e4a-4e87-a0f0-519ac51f91b6/
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
Related AI Lessons
⚡
⚡
⚡
⚡
Next.js vs Remix vs SvelteKit: Which Framework Should You Learn?
Dev.to · Etrit Neziri
Had my Frontend Developer interview with Capgemini (Application Developer) today, and I wanted to…
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Medium · JavaScript
🎓
Tutor Explanation
DeepCamp AI