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

Sectioning Our Site into Components

Task 3: Tutorial Based on Transcript

Sectioning Our Site into Components

Breaking your site into reusable components is a key step in mastering React development. This tutorial guides you through the process of creating separate components for different sections of a website, like "About" and "Contact," using modern JSX practices. Here’s how you can achieve it:


1. Setting Up the Components

Begin by creating files for the components in your project structure.

  1. In the src/components folder, create two files:

    • About.js
    • Contact.js
  2. Inside each file, set up a basic React component structure:

    import React from 'react';
    
    const About = () => {
        return (
            <section>
                {/* Add your About content here */}
            </section>
        );
    };
    
    export default About;
    
  3. Repeat for Contact.js, changing the component name accordingly.


2. Updating the Main Application

After creating the components, import and use them in the main client file.

  1. Open your main client file, likely App.js or a similar entry point.

  2. Import the components:

    import About from './components/About';
    import Contact from './components/Contact';
    
  3. Replace the JSX structure to include the components:

    function App() {
        return (
            <div>
                <About />
                <Contact />
            </div>
        );
    }
    export default App;
    

3. Transferring Existing Content

If your sections already exist in an index.html file or elsewhere:

  1. Copy the HTML structure for the "About" section.
  2. Paste it into the About.js file, converting HTML attributes like class to className.
  3. Repeat the same for the "Contact" section in Contact.js.

For example, an index.html snippet like this:

<div class="about">
    <h2>About Us</h2>
    <p>This is the About section.</p>
</div>

Would transform into:

<div className="about">
    <h2>About Us</h2>
    <p>This is the About section.</p>
</div>

4. Testing Your Components

Run your application to ensure everything works correctly:

  • Use the npm start command to launch your React app.
  • Verify that the "About" and "Contact" sections are displayed on the page.

5. Debugging JSX Issues

React strictly enforces valid JSX syntax. Common pitfalls include:

  • Unclosed tags: Tags like <br> or <img> must be self-closing as <br /> or <img />.
  • Mismatched tags: Ensure all opening tags have a corresponding closing tag.

Errors are usually displayed in the browser’s developer tools or terminal, helping you pinpoint issues.


6. Preparing for Reuse

By componentizing your sections, you can now:

  • Reuse components across multiple pages.
  • Add specific styles or functionalities to each section independently.

In the next tutorial, you’ll explore why composition is preferred over inheritance in React and how to make your components even more modular and scalable.


By the end of this tutorial, you’ve successfully broken down your site into smaller, more manageable React components. This modular approach is foundational to building dynamic, maintainable web applications. Let’s continue expanding on this skill in the next lesson!

Video Transcript

00:00Welcome back to our third section
00:02where we'll be componentizing a lot of stuff.
00:05And really in reality
00:06what we're gonna be doing is talking about good react
00:10strategy and practice to creating components beyond that.
00:13We're gonna do a lot of ES six.
00:16So this chapter is wonderful.
00:17You're gonna get a lot of e es six experience out of it
00:21and components in general.
00:22Alright, let's jump right into the first lecture
00:24because in the first lecture we're gonna be sectioning out
00:27our site into components
00:28or really starting to section them out in the last section.
00:33We built out the portfolio and really rebuild it out.
00:36And what I want do in this video right now is
00:40to build out both sections that are left into components.
00:44So let's go ahead and first start off by just going
00:47and creating those components.
00:49So I'm gonna go into my source, into components
00:51and I'm gonna create a new file.
00:53The first of them will be our about section.
00:56So lemme just go ahead and save it
01:00and let's create another one new file
01:03and the next file is gonna be our contact js
01:07and let's just go ahead and save it as well.
01:09Beautiful. So we have our two components.
01:11Now let's go into our client.
01:12We'll obviously we need to create them the next step.
01:14I'm gonna go and I'm gonna import our about
01:20from components.
01:26Oh yeah. You know, we still, yeah, that's
01:28what am I doing dot first, first Do then components
01:32slash about.
01:34Let's go ahead and copy this and also load up our contact.
01:41That's how we call it. Yeah, that's how we called it.
01:43Alright, beautiful. Now let's go into our portfolio
01:46and what I'm gonna do is I'm gonna just like import our
01:49react into our contact
01:53and we also want to import, uh, we want, want what?
01:56Where am I, uh, in our portfolio.
01:58I wanna go to our default export
02:01and just copy the same construct.
02:03You know what? Let's copy it all the way until here.
02:05And beautiful, all that's left for us to do is to make sure
02:09that we put here the name contact and let's just copy this.
02:14Oh, let's close that class as well and let's copy it
02:17and do the same thing in our about just making sure
02:21that we're sending out and about.
02:22Beautiful. Okay, so now that we've done that,
02:25the next step is actually to return the right thing.
02:27So let me again just open up here.
02:30Uh, let me just open up our HTML if we could find it
02:35in our public folder index.
02:37And let's first find our about section.
02:42And let me just cut it out.
02:44And you know what, I'm gonna even uh,
02:45just open up a new file and I'll just paste it here.
02:48'cause really what I wanna do is I wanna search
02:49and replace every place where there's a class.
02:52So lemme go ahead and do that.
02:53I'm gonna go find, search and replace
02:57and I'm gonna search for known.
03:00That's not what I'm searching for.
03:01I'm gonna search for class
03:03and I'm gonna change it to class name.
03:05And let's go ahead and replace all of them.
03:07And that did not work. So let's unselect
03:11and let's try again.
03:12File replace Class
03:17last into class.
03:19Name. Beautiful. Let's replace all. Beautiful.
03:22Now let's walk through it
03:23and also find any place where we have a a tag
03:25that's not completely closed.
03:27So this looks good. Okay, that was just our hr.
03:30So let's go ahead and copy that.
03:31And let's take this into our about section.
03:34And I'm just gonna go ahead and paste it right here.
03:37Save let's go ahead and do the same thing.
03:39Oso for our contact if we're already at it.
03:42And let's just grab everything in our contact
03:45and cut that all out.
03:47We're gonna go inside of here
03:49and for the exact same thing, I'm gonna go ahead
03:51and do a search and replace for class, class name, update
03:55that and run through all of the items here.
03:58First of all, I'm gonna get rid of the comments
04:00'cause they're not needed here.
04:02And um, our input needs to be self enclosed.
04:08There we go. And this input as well.
04:12And this looks good and this looks good as well.
04:17Beautiful. Now we're gonna just copy this
04:20and take it into our contact form
04:24or our contact section.
04:26Now lemme go ahead and save this
04:27and go back into our client.
04:29And in our client, let's go change our rendering.
04:32Instead of rendering only our portfolio,
04:34let's just create here a diviv as the root
04:37and then we'll put inside of it the portfolio.
04:44And we're gonna put in next
04:45after the portfolio, let's put our, what was it about?
04:51And next will be our contact And just to test it out
04:56and see that everything is working.
04:57What I wanna do, OTO, is let's just go into our index. HTML.
05:01Well not, we don't wanna go in here by, I,
05:03I just wanna override everything, the whole body.
05:04So we'll only see whatever we're working on.
05:06So let me just take that idea of our whole body
05:09and I'm gonna just put that into the ID
05:11that is being rendered instead of the React app.
05:14And let's go ahead and test it out.
05:16And now when we're looking at it,
05:17we should see our portfolio, our about section
05:21and where is our contact you?
05:25Oh, we have about twice. Okay, so we have your bug. Yay.
05:28Bugs are fun. So let's see what I, I've done wrong
05:30and we could already find it that i, I imported about again,
05:33which is a great reminder again that
05:35because we're using the default,
05:38we're using those default exports.
05:40The name itself doesn't really apply to the class.
05:42So really in this situation we're,
05:44we're exporting it without a name.
05:46It's waiting to get a name and it's getting that name here.
05:49So in this case we're pulling it about twice,
05:51giving it once the name about once the name contact.
05:54So let's fix that by typing your contact, save it,
05:58go back into our browser.
05:59And if we take another peak, then
06:02if we didn't have an error, let's see, what is our error?
06:05It couldn't parse. Oh, okay. Beautiful. Expected. JSX.
06:09So basically it's telling us that we didn't really close
06:11our JSX correctly.
06:12And it's more or less telling us that we,
06:15after the form, this is where our problems are beginning.
06:18If we take a look at our, where are we?
06:20If we take a look at our contact.
06:22So first of all, if I'm taking a look to try to see
06:24where our form starts.
06:26So we could see the form begins here.
06:28And if we go all the way down here, the form ends here.
06:32And then we have a diviv and we have a div and another div
06:36and another div and another diviv and another.
06:38So we need to have your three closing. 1, 2, 3.
06:41That looks good to me. Oh, and I see it. Our br here.
06:45We forgot. We forgot we have, that was a really hard bug
06:49because in the browser it didn't give us that, that error.
06:52And here we go, we have a contact form as well.
06:54Sometimes it gets hard,
06:55but sometimes the, the the, basically what it told us is
06:59that something in our XML is wrong.
07:00And unfortunately for us,
07:02it didn't really get explicit enough to tell us
07:04that it's our BR that wasn't closed.
07:06At least I didn't see that in the error message.
07:08And that's it. We've just created three separate components,
07:11but obviously this is not the end, just the beginning
07:13of the chapter because in the next section we're gonna take
07:16a look and see is there anything in our component
07:18that is reusable?
07:20And really in that case, how do we approach that?
07:22Do we create a parent class and then use inheritance
07:27or do we use something else?
07:28Which the answer is something else.
07:30And we're gonna talk about that in the next lecture.
07:33In this lecture we really started to think the way we want
07:36to think when we're creating components
07:38and react in the next lecture.
07:40We'll continue that process as we talk about why you want
07:43to use composition instead of inheritance.

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!