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!

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!

Sectioning Our Site into Components

Dive into React’s core strategy of breaking down a site into manageable, reusable components. Focus on structuring sections like About and Contact with JSX best practices.

07:46

Composition instead of Inheritance

Discover why React emphasizes composition over inheritance. Learn how to build reusable components with composition and simplify your React development process.

08:30

Dynamic Children from Model Data

Learn to extract model data in React and dynamically render components using ES6 for...of loop for scalable applications.

07:35

var, let, and const: Understanding Variable Scope in ES6

Explore ES6 variable declarations: var, let, and const, with practical examples for safer JavaScript coding.

07:27

Exploring ES6 Strings

Explore new string methods in ES6 like startsWith, endsWith, includes, and repeat. Learn how these tools improve efficiency and enhance dynamic React components.

05:16