⚛️ React Components

Chapter 4 of 15

What are Components?

Components are independent and reusable bits of code. They serve the same purpose as JavaScript functions, but work in isolation and return HTML via a render function.

💡 Think of Components as Custom HTML Elements: Just like you use <div> or <button>, you can create your own elements like <Header> or <UserProfile>!

Types of Components

There are two types of components in React:

Function Components

A Function component is a JavaScript function that returns HTML (JSX). This is the modern way to write React components:

function Car() {
  return <h2>Hi, I am a Car!</h2>;
}

You can also use arrow function syntax:

const Car = () => {
  return <h2>Hi, I am a Car!</h2>;
}
✨ Best Practice: Component names must start with a capital letter! This helps React distinguish between regular HTML tags and components.

Rendering a Component

To use a component in your application, use it like an HTML tag:

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Car />);

Class Components

A class component must include the extends React.Component statement and a render() method:

class Car extends React.Component {
  render() {
    return <h2>Hi, I am a Car!</h2>;
  }
}
📚 History Note: Before React Hooks were introduced in 2019, class components were the only way to use state and lifecycle methods. Now, function components with Hooks are the recommended approach.

Components in Components

We can refer to components inside other components. This is called composition:

function Car() {
  return <h2>I am a Car!</h2>;
}

function Garage() {
  return (
    <>
      <h1>Who lives in my Garage?</h1>
      <Car />
    </>
  );
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Garage />);

Multiple Components

You can use the same component multiple times:

function Car() {
  return <h2>I am a Car!</h2>;
}

function Garage() {
  return (
    <>
      <h1>My Garage</h1>
      <Car />
      <Car />
      <Car />
    </>
  );
}

Components in Files

React is all about re-using code. It's recommended to split your components into separate files for better organization:

Car.js

function Car() {
  return <h2>Hi, I am a Car!</h2>;
}

export default Car;

App.jsx

import React from 'react'
import ReactDOM from 'react-dom/client'
import Car from './Car.jsx'

ReactDOM.createRoot(document.getElementById('root')).render(
  <React.StrictMode>
    <Car />
  </React.StrictMode>
)
✨ File Organization: Create a separate file for each component. Use .jsx extension for files containing JSX. This makes your code easier to maintain and test!

Component Folder Structure (Vite)

A typical Vite React project structure might look like:

src/
├── components/
│   ├── Header.jsx
│   ├── Footer.jsx
│   ├── Car.jsx
│   └── Garage.jsx
├── App.jsx
└── main.jsx

Named Exports vs Default Exports

Default Export (One per file)

// Car.jsx
function Car() {
  return <h2>Car</h2>
}
export default Car

// Importing in Vite
import Car from './Car.jsx'

Named Export (Multiple per file)

// Vehicles.jsx
export function Car() {
  return <h2>Car</h2>
}

export function Truck() {
  return <h2>Truck</h2>;
}

// Importing
import { Car, Truck } from './Vehicles';

Component Best Practices

Real-World Example

Here's a more realistic example with multiple components:

function Header() {
  return (
    <header>
      <h1>My Website</h1>
      <nav>
        <a href="/">Home</a>
        <a href="/about">About</a>
      </nav>
    </header>
  );
}

function Content() {
  return (
    <main>
      <h2>Welcome!</h2>
      <p>This is my React application.</p>
    </main>
  );
}

function Footer() {
  return (
    <footer>
      <p>© 2025 My Website</p>
    </footer>
  );
}

function App() {
  return (
    <>
      <Header />
      <Content />
      <Footer />
    </>
  );
}
⚠️ Remember: Always capitalize component names! React treats components starting with lowercase letters as DOM tags.

What's Next?

Now that you understand components, you're ready to learn about Props - how to pass data between components!