02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Using Components inside of Components

Tutorial: Using Components Inside of Components

In this tutorial, you’ll learn how to build reusable React components by nesting components within each other. This is a foundational step in mastering React's component-based architecture, enabling cleaner and more maintainable code.


What You Will Learn

  1. How to nest components inside other components.
  2. Why componentization improves reusability and maintainability.
  3. Key ES6 features for working with React components.

Steps to Create Nested Components

1. Start with an Existing Component

Using the Portfolio component created earlier, review its JSX structure. Look for repeated or logically grouped elements that can become separate components.

Example:

<div className="portfolio-item">
  <img src="example.jpg" alt="Portfolio" />
  <div className="overlay">Overlay Text</div>
</div>

2. Create a New Component

Move repeated JSX into its own React component. For example, create a PortfolioItem component:

import React from 'react';

class PortfolioItem extends React.Component {
  render() {
    return (
      <div className="portfolio-item">
        <img src="example.jpg" alt="Portfolio" />
        <div className="overlay">Overlay Text</div>
      </div>
    );
  }
}

export default PortfolioItem;

3. Import and Use the New Component

Replace the repeated JSX in Portfolio with the new PortfolioItem component:

import React from 'react';
import PortfolioItem from './PortfolioItem';

class Portfolio extends React.Component {
  render() {
    return (
      <div className="portfolio">
        <PortfolioItem />
        <PortfolioItem />
        <PortfolioItem />
      </div>
    );
  }
}

export default Portfolio;

4. Test the Application

Run your React application to see the changes:

npm start

Check the browser to confirm that the Portfolio component renders multiple PortfolioItem components.


Common Issues and Fixes

1. Extending React.Component

Ensure all custom components extend React.Component to inherit React's functionality:

class PortfolioItem extends React.Component {
  render() {
    return <div>Content</div>;
  }
}

2. Uppercase Component Names

React distinguishes between DOM elements and components by their capitalization. Always use uppercase for custom components (e.g., <PortfolioItem />).

3. Valid JSX

All elements in JSX must be properly closed:

  • Self-closing tags: <img />, <hr />
  • Replace class with className to avoid conflicts with reserved keywords.

Benefits of Nested Components

  • Reusability: Simplify updates by isolating functionality into smaller, reusable components.
  • Readability: Break down complex UI structures for easier debugging and maintenance.
  • Scalability: Add or remove components without affecting the parent structure.

Summary

You’ve successfully created and used nested components in React! This approach promotes cleaner, reusable, and scalable code.

In the next lesson, you’ll learn how to dynamically pass properties to components, making them even more versatile.

For more tutorials, visit: https://02geek.com/category/react/reusable

Video Transcript

00:00Welcome to a new section
00:01where we will dive deep into the world of React
00:04and really understand its core competency
00:07and what really changes in the way you develop to be able
00:10to work within it.
00:12In this section, we're gonna go through the process
00:14of really learning it through experience,
00:18where we'll create components inside
00:20of components will pass properties into components,
00:24will pass even components into components
00:27and will change the state
00:29and properties of components, will understand
00:31what is a state, what is a property which are really
00:34critical for the world of React.
00:36And in general, by the end of this section,
00:39you're gonna have a really holistic good understanding
00:41of the three main competencies beyond React
00:44and also explore a lot of E ES six
00:47and explore a lot of building components in React.
00:52Okay, so in this lecture
00:53what we're gonna do is we're gonna look at how do you
00:56actually create a component
00:58or how do you use a component inside of a component.
01:01To do that, we're gonna think about the process of
01:04what components are,
01:05and already we could say, I could tell you that
01:08a big portion of React is the idea
01:10of componentizing every idea into separate component.
01:14The idea is reusability.
01:16The more components you have that are less aware
01:19of what's going on outside of them, the better
01:22and the easier it is for you to reuse components
01:24and to leverage the benefits of the architecture that stands
01:29behind of React
01:31through this lecture will also explore some ES six.
01:34Let's jump right into it.
01:35Let's just see components in components.
01:40Welcome back and it's time that we put all
01:42of our attention into React components.
01:45So we're gonna start off from the React component
01:47of portfolio that we've created in the previous chapter
01:50where really all we did was take a lot of HTML,
01:54which was the portfolio section, put it as XML
01:58as a return value for our render, which is a React component
02:02because, because our portfolio extends React component.
02:06Um, and we also made sure
02:07that we're not putting any illegal HTML things.
02:11One, we made sure that everything is closed.
02:13So hrs were self enclosed and images were self enclosed.
02:18And we also replaced the word name
02:19because uh, the word class because it's a reserved name.
02:23Alright, now if we take a really deeper look into our
02:26portfolio, well known as that, we have here a lot
02:29of repetition and part of the idea
02:31of React is CO to Componentize.
02:34That's exactly what we're doing.
02:35We created a component that is called Portfolio,
02:38but really we wanna look into this portfolio
02:40and see is there anything here that could be reused?
02:43And really when you're looking at this, you could see that
02:45inside of our row we have your different diviv
02:48elements that each have.
02:49And if we take a look at the site itself, it has an image
02:52with an overlay, which is controlled via CSS.
02:55Now what what we wanna do is we wanna make sure that
03:00why would we type all of this every single time?
03:02What if we wanna make some small change
03:04at some point in time?
03:06So really everything here should be dynamic.
03:09So what I'm gonna do is I'm gonna create this
03:11as a dynamic element
03:12and then try to see what are the things that are repeated
03:15or that are, that are different between each
03:18of those sections.
03:20So let's go ahead and create a new class.
03:22So I'm gonna go ahead and create your class.
03:24I'm gonna call this portfolio item
03:31And I'm gonna open and close around bracket
03:33and I've created a class.
03:35Now the functions, I just have to write render.
03:37So I need the render function.
03:39So I'm gonna open and close that. And there we go.
03:41I didn't have to write the word function again, this is one
03:45of those wonderful ES six features.
03:48Now I'm gonna return, what am I gonna return?
03:50Let's go ahead and return that item.
03:51So I'm gonna go ahead and grab,
03:54and I'm just gonna cut it right out
03:56and put it right in here.
03:58So we have your valid XML as well.
04:00So we don't have to worry about the structure
04:02because we already fixed it in the previous lecture.
04:04But now that we've created the CL this class,
04:06and notice very importantly that we have an uppercase P.
04:10This is incredibly important.
04:11This is just not a a, a nice to have. This is mandatory.
04:15Now this, because it's with an upper case,
04:18I could go in here
04:19and create a new element right inside
04:22of my actual portfolio itself with the portfolio item.
04:26Now what this is gonna give is basically gonna tell React
04:30and JSX
04:31and that for this matter to search
04:32for the class portfolio item
04:34and then place an item of
04:36that type exactly into this location.
04:39Okay? So all that's really left for me to do
04:43to test this out is to go ahead and save and test it out.
04:46But what I want to do is I'm gonna go ahead
04:47and place it a few times.
04:49So let's just copy it. So you could see
04:51that we really do see here
04:53that it's not just once, let's put it four times.
04:55So we'll have the exact same item four times.
04:57And if I go back into my application
05:00and I search for my item, I'll call for first.
05:03I can see, cannot call a class as a function.
05:06So let's see what's going on here. Cannot call a class.
05:10Oh, there you go. 'cause we forgot
05:11to extend the React component, right?
05:13I told you it has to be a React component right now it's not
05:16acting as that react component.
05:18So let's go ahead and save that.
05:20And in our browser, here we go.
05:22We got ourselves our first item four times
05:24before we can continue on to the remaining items.
05:27Beautiful. Now by the way,
05:29if we're already looking at these errors
05:30or messages, notice how we're getting a warning telling us
05:33that our rendering shouldn't be done directly to the body.
05:37And that is because a lot
05:39of times different libraries override things inside
05:42of the HTML
05:44and we really don't want anything ever
05:46to override things in our HTML.
05:48So React is giving you kind of like a notice telling you,
05:50by the way, be careful
05:51because React is in charge of any manipulation
05:54that is happening on the browser.
05:57Now, because we don't have anything on the browser,
05:59we really don't mind it too much.
06:01But really to avoid that error, just go into your HTML
06:04and in your body, just create a new diviv, um,
06:07and just give it a ID for your, uh, react app, for example.
06:12And once I have that React app, instead of instead
06:16of calling the top page, I could go ahead
06:19and in my client replace the top page from top page to be
06:24that React app that we've just created.
06:26Now, if I go ahead and go back into my browser,
06:28then we won't see that error anymore.
06:31Okay? So that is the basics of creating children.
06:35Now we're not done obviously because this won't be enough,
06:39and we really need to be able to fetch the dynamic things
06:42that are inside of here and make it dynamic.
06:45And that's exactly what we're gonna look
06:47at in the next lecture.
06:49In this lecture we explored how to componentize components
06:54and make them more efficient, less through using code.
06:57Instead of having it all cl clunked up
07:00together in the next lecture.
07:02We'll continue down this path
07:03because really for us to be able
07:05to create reusable components, we'll need to be able
07:07to pass in properties into those components.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!