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

Building a Reusable Container Class

Reusability is one of the cornerstones of React development. In this tutorial, we'll explore how to create a reusable container class using React and Bootstrap. This approach simplifies your application, reduces redundancy, and ensures consistency across components.


Why Reusable Visual Components Matter

Reusable components minimize repetitive code and allow for consistent styling and behavior. In this tutorial, we’ll focus on creating a container component that adapts to both fluid and fixed-width designs based on Bootstrap’s specifications. This will enhance the flexibility and efficiency of your React applications.


Step 1: Understanding the Container's Role

Containers in Bootstrap serve as layout wrappers. They come in two primary types:

  1. Standard Container: Has fixed widths that adjust at each responsive breakpoint.
  2. Fluid Container: Stretches to occupy the full width of the viewport.

Our goal is to dynamically handle both types within a reusable React component.


Step 2: Setting Up the Container Component

  1. Create the Component File

    • Create a new file named Container.js.
    • Copy the structure from the Jumbotron component, as the basic format is similar.
  2. Initialize the Component

    import React from 'react';
    
    class Container extends React.Component {
        render() {
            const cls = `container${this.props.fluid ? '-fluid' : ''}`;
            return (
                <div className={cls} style={this.props.style}>
                    {this.props.children}
                </div>
            );
        }
    }
    
    export default Container;
    
    • Use a template string to append -fluid dynamically when the fluid property is passed.
  3. Pass Through Props

    • Include styles and additional className props using props destructuring.
  4. Handle Children

    • Ensure any content nested within the <Container> is rendered via this.props.children.

Step 3: Integrate the Container

  1. Update the Jumbotron Component
    Replace hardcoded div containers with your new <Container> component.

    import Container from './Container';
    
    render() {
        return (
            <Container fluid={true}>
                {this.props.children}
            </Container>
        );
    }
    
  2. Test the Component

    • Add the <Container> to your app with different properties to verify its adaptability.
    <Container fluid={true} style={{ backgroundColor: 'lightgray' }}>
        <h1>Welcome to React</h1>
    </Container>
    

Step 4: Extend Functionality

  1. Support Additional Styling and Classes

    • Use the className and style props to ensure custom styles or additional class names are seamlessly passed to the container.
  2. Avoid HTML Attribute Errors

    • Ensure that invalid HTML attributes are not forwarded to the DOM by sanitizing props before passing them to the container.

Next Steps

In this tutorial, we’ve created a reusable container component that handles dynamic layouts with minimal effort. This approach simplifies your code and prepares it for scalable and maintainable development.

In the next lesson, we’ll tackle issues with invalid HTML attributes and learn how to handle them gracefully. Stay tuned!

Video Transcript

00:00You might have guessed it already when we used our button
00:03a few times, we actually made it already a reusable element.
00:07And that's really the mindset
00:08behind creating reusable visual elements.
00:11If there's anything visual that you need to reuse,
00:14in our case we're gonna need a container class
00:16that's gonna be reused if within the component
00:18or within other components.
00:20Then we'll extract it out into its own component.
00:23So in that process we're gonna take a look at how
00:25to create the container.
00:26But moreover than that, the importance of reusability
00:29and how to build reusable components.
00:32Alright, let's continue down the path.
00:34And really what we're trying to do is get our jumbotron
00:38to really be as dynamic as possible
00:40and the least amount of coding
00:42or copy that we have to add into our app.
00:45Now I already highlighted our container and that is
00:48because if you read the documentation of the jumbotron,
00:52which is provided with this title, um, as one
00:55of the references, you'll find out that the jumbotron,
00:58whenever it is fluid,
01:02whenever we add into it, uh, fluidness,
01:05where are we jumbotron fluid,
01:08then we need to have a container.
01:09This is, by the way, the word fluid is new to
01:14bootstrap version four.
01:15Okay? So with that said, what I wanna do, what I really want
01:18to do is I want to create a container class
01:20and then define it as it should be.
01:23'cause a container could have two different states.
01:25It could be a container
01:26or it could be a fluid container if the container
01:30was a fluid one.
01:31And in that case, if we take a look at our content,
01:34our content would span out throughout the
01:37whole air interface.
01:38Although technically speaking the differences are
01:42beyond unnoticeable in our, in our scenario.
01:45But in some scenarios it could come and become.
01:49So, but it really,
01:50in this case it really doesn't really matter.
01:52But still we want to build out our component to be able
01:55to deal with all the different types
01:57of containers that it can be.
01:59To do that, let's go ahead and create that new container.
02:02And by the way, this, what we're doing right now is oto
02:06gonna be how we actually solve a question
02:10that I raised earlier in the section where we saw how
02:13to reuse code by moving it into a utility package.
02:17If we could find it in our source.
02:18In bootstrap we have our utility package, contrary
02:22to non-visual things, how do you actually extend features
02:26inside of bootstrap or really inside of React?
02:30And the answer is, is that you don't, instead of
02:32that you do something that we've been doing here,
02:34which is basically we're using visual elements per our need,
02:39but without it using the inheritance.
02:42Whenever you do something like this, this is exactly
02:44what you're meant to do.
02:46So in a situation where we have a container, for example,
02:50this container is gonna be used
02:51throughout a lot of different places.
02:52So we wanna share this information
02:55among different libraries.
02:56For example, our jumbotron.
02:59To do that, we'll extract it out
03:01and create its own container logic.
03:03And I'm just gonna go ahead here,
03:04just copy everything in our jumbotron
03:07because our container is gonna be relatively similar.
03:10First of all, we're not gonna use the classify.
03:13Second of all, this is gonna be a container.
03:17Thirdly, we're not gonna import the properties.
03:20Fourthly, we're also going
03:22to set here a class without suffice.
03:25Let me just delete that
03:26and I'm also gonna delete all the
03:28other stuff that we had there.
03:29So basically we're gonna set up our class name.
03:33Now what I'm gonna do is I'm gonna go ahead
03:35and I'm gonna create a new cons, which I'll call class
03:39and we'll build out that class dynamically.
03:41Alright? But I'm gonna also use that new, uh, the new string
03:47template because it's just so much easier.
03:48And in any case, I want it to be a container,
03:52but I want it to be a container
03:54and append something to it only in a certain situation.
03:58What certain situation if this stop props is fluid.
04:03So if I'm sending in the fluid value, then I wanna append
04:06to it our fluid.
04:08If not, I'm gonna go ahead and send in nothing.
04:12So again, what I'm using here is I'm defining a variable.
04:16Once I have that variable, I'm basically checking is the
04:20variable fluid defined in our props?
04:23If it is not defined, then only container will come through
04:25'cause we'll connect container with an empty string.
04:28If not, we'll connect dash fluid to be more accurate.
04:32So let's go ahead and test this.
04:33So first of all, to be able to test it, I have
04:35to use this container.
04:36So I'm gonna go into my jumbotron and just
04:39before I use my children, let me go ahead
04:41and define a container.
04:44And I shouldn't forget Oso to load it in.
04:47So let me go ahead Oso and load it in.
04:49I want to import container from the current folder,
04:53written container.
04:55Beautiful. Last but not least, I'm gonna go into my app
04:58and delete the container 'cause it's not needed anymore.
05:01That's gonna be added dynamically.
05:03And if I go ahead and save this,
05:05go back into my application,
05:06everything hopefully should still work.
05:09But let's see what error we're getting.
05:10So if we take a DA dive to understand, so first
05:13of all you see our class name,
05:14but I actually never defined it.
05:16So let me go ahead into my container
05:18and send in our CLS constant that we've created.
05:23And if we take another peak,
05:24hopefully now everything works, which it does.
05:27And if we check in our inspector to see our container itself
05:30inside of our jumbotron, we will discover
05:32that we have a container.
05:34But if we went through and went to our jumbotron container
05:38and said it to have a property of fluid
05:42and saved it, we should now find our container having
05:47a still container.
05:49So let's see what's, what's, what's,
05:50what's, what's going on here?
05:51What's, what's going on here? Not happy.
05:55Okay, so fluid, let me, there's a double,
05:58double triple check that our container
06:00fluid is using actually that verbiage fufu lid.
06:06Hmm, I always do that type of stuff.
06:07Okay, so we fix that and that should be also fluid
06:11and let's go ahead and give it another shot.
06:15And now we can see that we have our container is now fluid,
06:18which is exactly what we hoped it to be.
06:21Now we don't have any content inside and that is exactly
06:23because we need a oso.
06:25Just like with our jumbotron
06:27where we're putting in the children inside, we need to pass
06:30through those children into this container as well.
06:32So just to the same way, I'm gonna set the children inside
06:36and that will sort that out.
06:37And what I've just done right now is I've created
06:40basically a functionality that doesn't have to live inside
06:43of the jumbotron and exactly that's
06:45what we do every single time.
06:46We have something that might be reusable
06:48that could stand on its own
06:49'cause containers are used throughout our application.
06:52Now one thing that I want to do is also I wanna make sure
06:55that if the user set an extra styling
06:57that those styles will come through.
06:59So to do that, I'm just gonna go ahead here
07:01and set here this props style.
07:05And one thing that's really powerful about this is if no
07:07style was sent through, then it will just not do anything.
07:11It's not gonna add the property
07:12unless the property has a value.
07:14So if I check my fluid item, I'll see
07:16that it's still exactly the same.
07:18But if I go into my container fluid and I add a style,
07:23and again we do that with opening, closing or curly bracket
07:25and doing that twice and let just set a color to be red
07:29and I save it and I go back into my application, we'll see
07:32that now it's gonna change the color
07:34and that style is gonna go into our style.
07:37Beautiful. So our styling here works, which is beautiful.
07:41One el. Another thing that we'd probably want
07:43to do is enable the class name to be able to pass through.
07:46If someone at some point would want
07:48to set here another class name, let's say of rat,
07:52how would we make sure that it goes through?
07:53Then we would have to in our container itself o
07:56to append the value that's inside there.
07:59So I'm gonna go ahead and put here a space, making sure
08:01that we could add in another definition
08:04and inside of our, our area
08:07that we could put here some code.
08:08I'm gonna go ahead and set here a question.
08:10Basically saying this props
08:14class name.
08:16Now if this value is undefined, I'm gonna say
08:19or put here at empty string.
08:21So what I'm doing here is checking
08:22to see if a class name is there.
08:24If it is there beautiful, it's gonna add it.
08:26If it's not there, then it's gonna go ahead
08:28and add an empty string.
08:30So if I go ahead and check back my into my application
08:33again, we should see now rat exactly
08:35where we want our rat to be.
08:37Lemme just word wrap again.
08:40So that's basically how it works
08:42and how we created a content fluid.
08:46A a, a container that could have different type of settings.
08:49In our case, I don't really need it to be fluid,
08:51I just need a container to go through.
08:53But it's gonna open up
08:54A door for another challenge,
08:56which we'll talk about in the next lecture, which is fine.
08:58We created a container that could be fluid,
09:00that could have properties.
09:02We really don't wanna let anyone, when they're working
09:04with a jumbotron to gain access to classes
09:07inside of their container.
09:08They don't need to. They could use CSS to ask for their to
09:11for the element if they want something
09:13specific to change in it.
09:14But we do wanna give them an option
09:16to turn it in to be fluid.
09:18And to do that we're gonna have to change our logic flow
09:20and we'll talk about that in the next lecture.
09:24In this lecture we created
09:26and understood even more in depth the importance
09:28of reusability and react.
09:30In the next lecture, we'll move back.
09:33There's a problem whenever we're sending properties,
09:35if those properties are not valid, HTML attributes,
09:39well we're gonna get errors and we don't want errors
09:41and we're gonna solve that 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!