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

Composition instead of Inheritance

Understanding Composition Instead of Inheritance in React

In this lesson, we delve into the foundational principles of React's design philosophy, focusing on why composition is preferred over inheritance. Composition simplifies code, enhances readability, and promotes reusability, making it a cornerstone of React development.


Why Not Inheritance?

React discourages inheritance due to its complexity and debugging challenges, especially in deeply nested hierarchies. Inheritance often obfuscates the relationships between components, making the code harder to maintain and understand.

Instead, React emphasizes composition, which involves assembling components together like building blocks. This approach is clearer, modular, and easier to manage as applications grow in size and complexity.


Creating a Section Component

To illustrate composition, we refactor sections of a React application into reusable components:

  1. Identify Reusable Structures: In our example, the About, Portfolio, and Contact sections share common structural elements.
  2. Build a Base Component: A new Section component encapsulates shared properties like id, className, and child content.
  3. Integrate with Existing Components: Each section (e.g., About) passes its unique content and attributes (like title) to the Section component via props.

Benefits of Composition

  • Simplified Code: Extracting shared elements into a single component reduces duplication and makes individual components cleaner.
  • Enhanced Flexibility: Each section customizes its behavior by passing props, enabling distinct functionality without altering the core component.
  • Improved Reusability: The Section component can be reused across various parts of the application, adhering to the DRY (Don't Repeat Yourself) principle.

Dynamic Content Integration

Through props like children, the Section component dynamically renders unique content for each section. This demonstrates the power of React's declarative approach and further solidifies why composition is favored.


In the next lesson, we pivot slightly to discuss modern JavaScript practices, exploring var, let, and const in ES6. This will build foundational skills for managing variables effectively in your React applications.

Video Transcript

00:01React doesn't encourage you to use inheritance.
00:04So if you know inheritance, wonderful
00:06but it's not encouraged in React.
00:09A lot of it is because inheritance is incredibly complicated
00:12to debug, especially when you have a lot
00:14of inheritance going on.
00:16Very hard as a developer to figure out what's going on.
00:19A lot of the structures in React were intended
00:21to make the code look as simple as possible
00:25and composition makes things a lot easier to comprehend.
00:29So in this lecture we're gonna take a look at basically why
00:32comprehend why it's more easier to to to comprehend
00:35and we're gonna see it an action of
00:36how do you create compositions instead
00:39of using inheritance when you're working
00:41with your React components.
00:44Traditionally in programming and object-oriented programming
00:48and JavaScript Otto,
00:50you know when you have common commonalities such
00:53as these sections, they're all sections
00:55and it makes sense to create a class
00:58that's all about sections that would then have all
01:01of the things that sections have
01:03and then derive out it through inheritance.
01:06Go on and and implement specificities that relate
01:09to an element, but really in react
01:12and generally speaking in my perspective
01:14and programming in general, you're better off
01:17with composition
01:18and that's exactly what we're gonna do in this lecture.
01:21Now composition is almost always,
01:25you could almost work with com,
01:27almost always use composition instead of inheritance.
01:29And it makes things a lot more visually clear what they are.
01:33Now we're still gonna create a section
01:35and to do that I'm gonna create a new file
01:37and I'll call it section and I'll call it section js
01:42and let me just go ahead and save it.
01:45Now section js is literally gonna be a component just like
01:48the about section, but instead of being an uh,
01:52a whole component,
01:53what it's actually gonna be is it's gonna be basically a
01:56you, you could say a representation of all the things
02:00that don't really change
02:01or are shared commonalities between everyone.
02:04So if we take a look at this, this is the content of
02:08our about section.
02:10So that's not really part of our element, right?
02:13What we would have here is actually that content,
02:16but when we look at all the rest,
02:17almost everything else in here is kind of relatable.
02:21Now, not a hundred percent but kind of relatable.
02:23Now I'm gonna call this section.
02:26Now we're gonna implement it
02:28as we implement it into our about section.
02:31And what I wanna do in the about is basically
02:34replace our full section here
02:37and instead of that I'm gonna go ahead
02:38and I'm gonna import our section, which is from
02:43related to the current folder that I'm in.
02:46I'm looking for section and what are we gonna return?
02:51We're gonna return a section. Beautiful.
02:55So that's what we're returning.
02:56We're returning a section, but not
02:58Only are we returning a section,
03:00we're gonna return a section
03:01and put inside the actual content of our about section.
03:06So we're just gonna put that content right into the section
03:11and let's also send in all of the dynamic elements
03:15that all the things that we really want
03:16to have here that will be dynamic.
03:18So first of all, everything here, our ID and our class name
03:22and stuff that we wanna send in.
03:23So I'm just gonna go ahead and send that in.
03:25Um, from a, from a level of content, we wanna make sure
03:29that our about changes as well
03:31because that is the title there.
03:32So I'm gonna go ahead and set a title
03:34and send it to be our about.
03:37Now let me just word wrap here.
03:39View word wrap and I'm gonna go ahead
03:42and delete our section.
03:44So just going over what we've just created here.
03:46If I just run through it really quickly,
03:49I created a new section.
03:50The content of that section is really only the dynamic part
03:53of our element.
03:55Beyond that, all the rest is sitting and living.
03:58Within the our section, we're sending in all all
04:01of our Dyna dynamic attributes,
04:03which was our class name ID and title.
04:06Now if we go into our section,
04:07we really want to implement this.
04:09So if we go back into our section, first of all
04:11for our class name, instead of sending in a string,
04:14we're actually gonna pass along the value from this stop
04:17props class name.
04:21And for our id, we're gonna do the exact same thing.
04:23So we're gonna pass along the id literally just having it
04:28props class, not class.
04:30Now it's id id. And last but not least, well almost last
04:34but not least, we wanna change our about to be our this
04:38props and title.
04:40Now the last last part that's left is to make sure
04:43that the content of our about section, the content
04:46of this section will be printed out exactly here
04:49inside of this row.
04:50To do that, I'm gonna call our this props children.
04:55And that's all I had to do
04:56to make sure my about section is rendering
04:59with our new section model.
05:02Now all the uh, uh, let me just check
05:03and see that we don't have any errors and we sure don't.
05:06And our about section has been rendered beautifully.
05:10Alright, let's continue
05:11and implement our new section we've created
05:14for our portfolio and also for our contact me
05:17or our contact form.
05:18So let's start with our portfolio.
05:20I'm gonna go ahead and open it up
05:22and in our portfolio, what I'm gonna do is again,
05:24going into our portfolio item itself,
05:26which is already now quite nice.
05:28I'm gonna go ahead and change it
05:30to be an uppercase section notice by the way,
05:32very importantly, notice how our section itself, um,
05:37when it's lowercase, it's regular HTML when it's uppercase,
05:40it's not regular HTML anymore,
05:42but it's, it's actually looking for a class.
05:45So we have here our portfolio, we have a title as well.
05:48We're not sending in a class name on contrary to our contact
05:51where we had a where are you?
05:54No, not contact our about section where
05:57We had a class name of success that we're sending in.
05:59In the case of our portfolio, we're not doing that.
06:02Uh, but instead of that we're only sending in our
06:04title of portfolio.
06:06Now all this stuff is already not relevant,
06:08but we are sending in the children.
06:10That is true still,
06:12but we don't have all this content anymore,
06:15nor do we have this container anymore.
06:17So it's literally just a section with its children,
06:20which is so much cleaner.
06:21Look how beautifully simple this is.
06:23At this stage now through the art
06:25of composition, we're composing it.
06:28All we have to do is we have to make sure
06:29that we're importing just like we do in our about,
06:32we are importing our section into our portfolio.
06:36Now let's go ahead and do this oso for our contact
06:40before retest things out.
06:41So again, for our contact that we're gonna, again,
06:43we're gonna use a section instead we're gonna set a title
06:48property and send in the actual title
06:49that is going to be used.
06:52And we're only gonna put in the content itself without the
06:55actual row.
06:56So here we go. So this is the content of the section
07:01and we wanna make sure that we are,
07:04what happened to my mouse?
07:05My mouse fell asleep. So we'll use no mouse
07:07and mouse come back.
07:09I need you. All right, no mouse. Oh, we're back. Beautiful.
07:13So now let, let me just make sure that I'm, uh,
07:16not putting too many divs.
07:17So the form is here. I have one
07:18diviv and then we have the section.
07:20So making sure that we close the section
07:22with an uppercase letter as well.
07:24'cause again, we're looking for a class,
07:25it's not actually HTML sections.
07:28Um, that's it. Now it's time for us to test it out
07:30and see if it actually is working.
07:32And if we take a peek,
07:33we'll see our portfolio is still working,
07:35our about is still working
07:36and our contact me is still working.
07:38And the beauty of this composition that we've done,
07:40that we've composed inside the section,
07:43we didn't need inheritance.
07:44Things are very clear
07:45because we could see what we've added into our component
07:48and as we find more things that are reusable,
07:51we could extract them into separate
07:52classes and then use them.
07:54Now in a relatively unrelated topic,
07:58but completely related, I wanna talk about another,
08:00something that relates to E ES six, kind
08:02of like a break from learning about composition.
08:04And in the next lecture I want
08:06to teach you a little bit more about variables
08:08and how do they work in e ES six
08:10and the types of variables that are available.
08:13In this lecture we really made the case for composition
08:16and so how to use composition in react.
08:19In the next lecture, we'll continue down the path
08:21of creating components as we look at
08:24how do we create dynamic children that are derived out
08:28of model data.

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!