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

Dynamically Passing Components into Components

Tutorial: Dynamically Passing Components into Components

In this tutorial, we will explore how to dynamically pass components into other components in React. This technique enables greater reusability and modularization in your React applications. Additionally, we'll delve into some ES6 concepts like destructuring and imports/exports, which further enhance your development workflow.


Understanding Components with Children

React components can contain children, much like HTML or XML elements. These child components can be dynamically passed and rendered, making your code more flexible and reusable.


Step 1: Setting Up a Parent Component

  1. Start with a parent component, such as Portfolio.

    • Ensure it has a render method that includes a placeholder for child components.
    render() {
        return (
            <div>
                {this.props.children}
            </div>
        );
    }
    
  2. By using {this.props.children}, React automatically renders whatever child components are passed into Portfolio when it's used.


Step 2: Passing Child Components Dynamically

  1. In your main application or another parent component:

    • Import the Portfolio component.
    • Add Portfolio to your JSX and include multiple child components inside it.
    import Portfolio from './Portfolio';
    import PortfolioItem from './PortfolioItem';
    
    function App() {
        return (
            <Portfolio>
                <PortfolioItem img="cabin.jpg" />
                <PortfolioItem img="cake.jpg" />
            </Portfolio>
        );
    }
    
  2. Each PortfolioItem represents a unique child component with its own props.


Step 3: Exporting and Importing Multiple Modules

  1. Use named exports to export multiple components from the same file:

    export { Portfolio, PortfolioItem };
    
  2. Use destructuring to import multiple components in your application:

    import { Portfolio, PortfolioItem } from './Components';
    
  3. For components with a default export, you can rename them during import:

    import DefaultPortfolio from './Portfolio';
    

Step 4: ES6 Destructuring for Cleaner Code

Destructuring simplifies working with objects, especially when passing props.

  1. Inside the PortfolioItem component:

    • Destructure the props object to directly access values.
    function PortfolioItem({ img }) {
        return <img src={img} alt="Portfolio Item" />;
    }
    
  2. Use destructuring in imports to selectively extract the required modules or components:

    import { PortfolioItem } from './Components';
    

Step 5: Dynamic Ordering and Rendering

Since child components are passed dynamically, their order and presence can be changed without modifying the parent component logic. For example:

<Portfolio>
    <PortfolioItem img="submarine.jpg" />
    <PortfolioItem img="cake.jpg" />
    <PortfolioItem img="cabin.jpg" />
</Portfolio>

By reordering the PortfolioItem components, you dynamically control the rendering order.


Key Concepts Covered

  • Props.children: Allows components to dynamically render their child elements.
  • Destructuring: Simplifies accessing and using object properties in ES6.
  • Named and Default Exports: Efficiently organize and import components or modules.
  • Dynamic Components: Enhance modularity and flexibility in React.

Summary

By passing components into components dynamically, React enables a highly reusable and modular code structure. Combined with ES6 features like destructuring and export/import techniques, your applications become easier to manage and extend.

In the next tutorial, we’ll explore how state differs from props and how to use it to create truly interactive components.

Video Transcript

00:00In the last lecture we were introduced to props
00:03and how we could use them
00:04to send properties into our component.
00:08In this lecture, we're still in properties,
00:10but this time around we're taking a look at
00:12how do we actually send components into components.
00:15We're gonna basically say, oh, now you are aware
00:18that components could have children just like an HTML
00:22or XML.
00:23We're going to through that understanding,
00:25let's start componentizing our components
00:28and we're gonna learn in E es six how to import
00:30and export classes
00:32and share them to make our code much more reusable.
00:36Moreover than that, we're also gonna learn about object
00:39destruction or disruptors, which is something new
00:41to E ES six that makes really coding so much easier
00:44and it's one of the greatest additions into ES six.
00:47So let's jump into it and let's learn more about components
00:50and a lot more about ES six.
00:55Alright, let's continue making our component even better.
00:58And really our biggest problem in our component right now is
01:02that really outside of the component,
01:04we're just calling a portfolio.
01:06But in reality what's actually happening
01:09is our portfolio itself.
01:11And by the way, I noticed here I have a small type, it's not
01:15Port Folio
01:16but port, port, port, yeah.
01:21Okay, portfolio. There we go.
01:23Um, but it didn't really matter by the way,
01:24because we were exporting this class
01:27and notice what we did inside of the client here.
01:28We redefined the name. We could have called this anything.
01:31We called it call this love.
01:33It really didn't matter
01:34because once it was outside of that package,
01:36that default package could have been called anything
01:39for our case, we called it portfolio.
01:41But literally keeping it the way it is would basically keep
01:44everything looking exactly the same way.
01:46So let's revert it back, let's call it
01:48portfolio 'cause that's what it is.
01:49But really our problem right now, beyond that uh,
01:53spelling error, that was really not an issue.
01:55Our problem is, is that we're baking inside here all
01:58of these elements and in reality we would love
02:00to place them right outside, inside of our client here
02:03so we could then change them whenever we want to.
02:06So the portfolio itself, would it actually bake inside of it
02:10how the portfolio should look like?
02:12It would just have the portfolio functionality.
02:15So before we do that,
02:16I wanna teach you about a new something that is Newt ES six,
02:19which is called destructing.
02:20And with destructing we're gonna get that to happen.
02:23So first of all, let me just, uh, word wrap everything here
02:26'cause I just wanna and let go ahead here
02:28and I want to create here a variable,
02:31I'm just gonna call it creator here.
02:32An object that will mimic the behavior
02:34of this function right now.
02:36So I'm gonna have your A from uh,
02:38and the from is gonna be Ben and I'm gonna have also a two.
02:42And that two is gonna be you. Now wouldn't it be lovely?
02:46And a lot of times when you're working with functions, uh,
02:49it's quite frustrating that sometimes you have to have a lot
02:52of values that just don't have anything inside them,
02:54but only some of the values you need to send.
02:56So for example, if you wanted to send two
02:58but wanted to keep the default here, it would be quite hard
03:01'cause you would have to send that default in or send null
03:04and so on and so forth.
03:05And disruptors are a really wonderful way
03:07to make our code more clean and easier to consume.
03:11Now the way that you work
03:12with a a disruptor, I wanna show it to you.
03:14Basically you create a a a just like the object constraint,
03:18which a curly bracket open
03:20and close one in the variable definition itself there you
03:24say, what are the values you wanna extract?
03:27So there you go. Here we're distracting from and to and
03:30after we destruct, we we want to say,
03:33so whom are we destructing from?
03:35And we could, this is the construct
03:37that will basically take the variable object
03:40and extract out of it or destruct it and turn
03:43and get us only the variables from.
03:45Or two, we could have also said we only want from,
03:47or we only want two or we only want something else.
03:50And if I do your con con log for the from
03:55and two, we should now get those two values separated
03:59and extracted out.
04:00So if uh, we take a look at here, Ben, you beautiful.
04:03So let's continue down this line. Let's see how you do.
04:06You do this inside of a function, which is quite cool
04:08that you could, so you could literally
04:11destruct a function right inside of the definition
04:13of the function itself.
04:15And literally all I'm gonna have
04:17to do here is I'm just gonna, um, there,
04:18there won't be any defaults anymore in our case
04:21and I'm just gonna list out that I want,
04:23although I could have your default.
04:24I want from and I want to.
04:25And if they're not sent in, then those are the values
04:27that would be inside of them.
04:29Now if I go ahead
04:30and instead of uh, um, I'm gonna do a console log
04:35and this time around I'm just gonna go ahead
04:37and I'm gonna send in that object.
04:39So I'm gonna send hey, with our object
04:42and let's also make sure that this is gonna be Ben two
04:45and this should be you two.
04:47And if I go ahead here
04:48and click your honor refresh, we should see
04:51hello from Ben to to you two.
04:54Now if in any, any situation I actually didn't have two,
04:57there is no value there.
04:58It was never created. And let me just comment this out here
05:01because we're not really extracting anything there
05:03and we go back into our application, we're gonna see
05:05that when it re renders itself, it's gonna basically take
05:07that default value.
05:09So we could basically create a default
05:12and also deconstruct it, which makes it really powerful
05:15'cause then we could just send in whatever we want
05:16to without needing to worry about the order
05:19of which it's going to be sent in.
05:21So those are disruptors.
05:23Now with the help of disruptors, we're gonna go ahead
05:25and also implement our portfolio.
05:27'cause we really want to import not only portfolio,
05:30but we also want to import our portfolio item.
05:34Now to import our portfolio item, again, I'm gonna go ahead
05:37and type your export.
05:38And export is gonna make sure
05:40that portfolio item is being exported
05:42but contrary to our default export, which basically means
05:46that our default doesn't have a name.
05:48Really this name is not relevant really
05:50for the sake outside.
05:52Our portfolio doesn't have a name
05:54and we have to explicitly request it to gain access to it.
05:58Now the way we would do it,
05:59and if we don't have here a default,
06:01but I'm gonna keep the default.
06:02If we didn't have the default,
06:04let me just show you how we do this.
06:05Again, we destruct and the portfolio wouldn't be true
06:08because there is no portfolio technically,
06:11although there is it's portfolio,
06:12which is the default as well.
06:14And I'm gonna go ahead here and I could comma
06:17and send out, okay, I want my portfolio item as well.
06:22Now on contrary
06:24to like maybe the portfolio was not called portfolio,
06:27I could also say I want the default
06:29because there is a default
06:31and I could say I want the default as portfolio
06:33or as anything else.
06:35And any value that I send you, I could say
06:37as something else, as item if I wanted to.
06:39But portfolio works perfectly for me.
06:42Now that I have access to portfolio item
06:45and portfolio, I could go ahead now
06:47and extract this portfolio item.
06:49So I'm gonna go ahead and copy that out, save it.
06:52I'm gonna go into my client and I'm gonna open
06:55and close my portfolio.
06:58Lemme just break a few lines so we're not in the edge
07:01of a screen and lemme just make this a little bit cleaner
07:04and we could paste inside our code.
07:07Lemme just push it out a bit. So there we go.
07:09So now we pushed it through.
07:11Now it's not gonna actually run,
07:12but I'm hopefully not gonna get any errors.
07:14So if I run this, so it's going to work,
07:17although it's not gonna show us the portfolio,
07:19why is it not showing us the portfolio?
07:20Because we never really towed the portfolio
07:22that we wanna render anything.
07:23So we need to tell it where we wanna rent those, we
07:26where we wanna render those children.
07:29Now to do that, I'm gonna access the property
07:32of the portfolio as well.
07:34And again, I'm gonna, because I'm doing this inside
07:36of a jsx area, I'm gonna open and close a curly bracket
07:40and I'm gonna type for the variable name,
07:42which is called Vista step props dot children.
07:45And that's all I needed to do to paste inside
07:48of here the children that I want to have inside here.
07:51Going back into my application, I'll see that all
07:54of those children have been created.
07:56Moreover, now I have more control over deciding
08:00what I want in what order.
08:01So now maybe for a certain page I want
08:03to use the same portfolio,
08:05but I'm gonna have a portfolio for X things
08:07and a portfolio for Y things.
08:09So let's say I wanna move the cabin down the line,
08:11so it's not gonna be first anymore.
08:13So now when it re-render,
08:14it's not gonna be first, it's gonna be last.
08:17And there we go. We learned quite a lot of stuff.
08:18We learned how to deconstruct objects as we were looking at
08:22that in our client where we created an object
08:24and then we could de deconstruct it.
08:26Incredibly useful.
08:27We've seen it also how we could use it within functions.
08:30And we've also saw
08:31how we could use those deconstructions in those
08:34exports and imports.
08:36Last but not least, we saw also
08:38that we could export more than one thing.
08:39And by the way, it doesn't have to be a class,
08:41it could be also a function and even a variable.
08:44So we could, whatever we want to export, we could export
08:47and we have only one default.
08:49So if I just went ahead
08:50and I just used just one name inside
08:52of here without deconstruction, I would get
08:54The default value.
08:56So that's it. In this lecture we explored a lot
08:59of e es six things
09:01as we integrated children into our components.
09:04In the next lecture, we're going to move away from,
09:07from components and well, not from components,
09:09but from properties to talk about state.
09:12As we said, there is a difference
09:13between properties and state.
09:15And that's exactly what we'll explore in the next lecture.

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!