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

Creating the Jumbotron Component

The Jumbotron is a key visual element in Bootstrap that grabs user attention with its bold layout and design. This tutorial guides you through creating a reusable Jumbotron component in React, integrating it seamlessly with children and dynamic properties. By the end, you’ll have a versatile Jumbotron component ready for your projects.


What is a Jumbotron?

A Jumbotron is a Bootstrap component used to showcase key content on a web page, typically as a large banner with text and visuals. When built in React, it becomes a reusable powerhouse, enabling you to dynamically pass content and properties while maintaining a clean, scalable architecture.


Steps to Create a Jumbotron Component

1. Set Up the File Structure

Create a new file for the component:

touch Jumbotron.js

2. Import Dependencies

Begin by importing React and the classify utility function you created earlier:

import React from 'react';  
import { classify } from './utils';  

3. Define the Component

Create a class-based component named Jumbotron that extends React.Component:

export default class Jumbotron extends React.Component {  
  render() {  
    const className = classify('jumbotron', this.props.className);  
    return (  
      <div className={className} {...this.props}>  
        {this.props.children}  
      </div>  
    );  
  }  
}  

Here’s what’s happening:

  • Dynamic Classes: The classify method ensures that the Jumbotron always has the correct Bootstrap class while allowing dynamic additions.
  • Children Handling: The {this.props.children} allows you to embed custom content inside the Jumbotron.
  • Prop Spreading: The {...this.props} passes all additional properties to the root div.

4. Use the Component

Import the Jumbotron into your app and use it as a wrapper for content:

import Jumbotron from './Jumbotron';  

<Jumbotron className="custom-style">  
  <h1>Welcome to Our Site</h1>  
  <p>This is your first step towards mastering React.</p>  
</Jumbotron>  

Key Features

  1. Dynamic Styling: Automatically includes Bootstrap’s jumbotron class while allowing for custom classes.
  2. Reusable Structure: Pass any content using props.children, making the component flexible for various use cases.
  3. Seamless Integration: Supports additional properties like id and data-* attributes without extra coding.

Best Practices

  • Leverage Utilities: Use your classify utility for consistent styling logic.
  • Keep It Lightweight: Avoid embedding non-essential logic directly into the component.
  • Test Extensively: Test with various child elements and props to ensure the component behaves as expected.

Conclusion

You’ve now built a reusable Jumbotron component that integrates with React and Bootstrap effortlessly. By separating logic and focusing on dynamic reusability, this component is primed for any project.

In the next tutorial, we’ll explore how to make this Jumbotron even more dynamic by incorporating reusable visual content. Let’s keep building!

Video Transcript

00:00This lecture is quite straightforward.
00:02We're gonna go ahead and create our jumbotron
00:04component from scratch.
00:06And in the process we'll take a look at that process
00:08between Bootstrap React and Jumbotron Children.
00:12So let's jump right into it.
00:14Alright, let's continue and create our jumbotron.
00:18And our jumbotron is gonna be incredibly easy to create.
00:21I'm just gonna go ahead and create a new file
00:24and call it jumbotron js.
00:27Save that file. And once I have that saved file,
00:30literally I'm just gonna go ahead
00:31and copy, you know, let's just do it from scratch.
00:33'cause we're relatively new to this whole world.
00:35So import, first of all, we want to import React from React.
00:40Okay, that's, that's good.
00:41The next thing we want to import is we also want
00:43to import our new utility that we've created
00:46that was called classify.
00:48Lemme just copy that instead of typing again
00:51from the current director Utils.
00:53So we have our, our jumbotron.
00:56The next thing is I wanna export default new class,
01:00which is gonna be called Jumbotron
01:04and it's going to extend
01:06react component.
01:10There you go. So that's a basic construct,
01:13which is literally the same thing here.
01:14The only differences is really the name of
01:16what we're exporting.
01:18And we're also going to have a render method
01:21spit out when we are ready.
01:25So basically we have your render
01:27and we're gonna send out our render where we would return.
01:30And now it's time for us
01:31to create something a little bit different.
01:33So in our application itself, first of all,
01:35let me just go ahead and copy our diviv layer
01:38and I'm just gonna go ahead and just paste it right there
01:42and close our div layer.
01:44And let me just also make sure that I'm in a word wrap.
01:47And first of all, I'm gonna also already right away get rid
01:50of any reference of, well, any reference
01:54of our jumbotron there.
01:55But really I'm gonna go ahead and I'm gonna go ahead
01:58and automatically, first of all, for our style,
02:00we're gonna have that sent in dynamically.
02:02We don't need that there at all.
02:03But for our class name, what I want to do is I want
02:06to capture the actual values that are being sent in here
02:09and I want to basically wrap them in the classify method.
02:14And I also wanna make sure
02:16that I'm ascending in the jumbotron value with the under
02:20with a lowercase J.
02:22Just as a reminder, in our util it expects a key
02:25and the value, which is the actual, in our case,
02:28would be this props class name.
02:32So if I go ahead and test this, obviously,
02:34well this is not gonna work yet
02:36'cause I need to do two more things.
02:38For one in my app itself, I need to make sure
02:40that I'm importing our jumbotron from
02:47bootstrap jumbotron, making sure that I have a uppercase J.
02:50Incredibly critical.
02:52By the way, I could have called you anything,
02:53I could have said Nafi and everything would work,
02:56but I have to have an uppercase N if
02:58I would choose that. That's
02:59the name I want. The name internally really doesn't
03:02matter because once I put the word default,
03:04this is being sent.
03:06You could change a name whenever you want to.
03:08In our case, we're happy with the name
03:09that we've created of Jumbotron.
03:11So we're gonna stick with jumbotron
03:13because that's really the name that we want everyone to use.
03:17Okay, so now that we've imported it, I could go ahead into
03:20and change my diviv layer, my external diviv layer, go ahead
03:24and open and close with our jumbotron.
03:27And I could go ahead and get rid of all of the excess usage
03:31of jumbotron as lingo.
03:33Uh, the style we'd still wanna put there
03:35'cause that's dynamically added in.
03:37Now if I go ahead and save this,
03:39we're gonna see something really weird,
03:40which will make a lot of sense to you in a minute.
03:42So I'm just making sure that everything here is saved
03:44before we go into our browser to test things out.
03:47So once we go into the browser,
03:48you'll see we're not getting an error,
03:50but we're getting an empty jumbo chart.
03:52You could see that gray line there.
03:53Now the reason is, is
03:55because we need to implicitly say inside of our component,
03:59where do we wanna place its content?
04:01And in this case its content.
04:03Is this Diviv container to be able to place that content.
04:06What I'm gonna do is I'm gonna go ahead here
04:08and add in a curly bracket.
04:11And I'm gonna say this props children,
04:14which is gonna give me the actual content that's inside
04:17of the actual jumbo tron itself.
04:20And then, and only then once I save
04:23that we should get our content back in and working.
04:27Now we only have a little problem here is
04:29that our image is not following through, is not working
04:32as we would hope for it to work.
04:34And if we take a look into our app to try to recognize
04:37and understand what's actually going on here, we will find
04:41that, that there is absolutely nothing
04:43wrong with our code here.
04:44But what we're actually missing is in our jumbotron itself,
04:47we're not actually doing what we've done in the button,
04:50which was basically to word reps.
04:53You guys could see it. Um, was basically the Vista props,
04:57which were basically extended and basically told our class.
05:00And I'm gonna do that before the class name
05:02'cause we wanna override the class name as you remember.
05:051, 2, 3. And I wanna say A, all the props you're sending in,
05:09make sure that they're crossing through as well.
05:11Now once we've done that as well,
05:13then we should find now our full component, just the way
05:17we've intended for it to be.
05:21In this lecture we created the foundations
05:23for our jumbotron component.
05:25And in the last one, next lecture,
05:27we're gonna go back talking about how do you reuse content.
05:30This time around it's gonna be visual content.

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!