Chapter 3: Components in React

Components in React — this is where React really starts to feel powerful and fun.

Today we’re going to cover:

  • Functional vs Class components
  • Creating and nesting components
  • Component composition
  • Exporting & importing components

I’ll explain everything slowly and clearly, like a patient teacher walking you through it step by step. We’ll use plenty of complete, runnable examples you can copy-paste into your Vite project right now.

1. Functional vs Class Components (2026 Reality)

In modern React (React 18 → React 19 in 2026), almost everyone uses functional components + Hooks.

Class components still exist and work perfectly, but they are considered legacy for new code.

Aspect Functional Components (Recommended 2026) Class Components (Legacy / Special Cases)
Syntax Plain JavaScript function ES6 class that extends React.Component
State useState, useReducer this.state + this.setState()
Side effects / lifecycle useEffect componentDidMount, componentDidUpdate, componentWillUnmount etc.
Readability Cleaner, shorter, less boilerplate More verbose, this binding issues
Performance Slightly better (no this overhead) Similar in practice
Modern features Full access to all new hooks & React 19 features Cannot use hooks directly
Error Boundaries Not natively (but can wrap class component) Native support via componentDidCatch
When to use in 2026 Almost everything — new code, libraries, apps Only for error boundaries or very old codebases

Official recommendation (React docs + community 2026): Use functional components for all new work. Class components are still supported forever (no plans to remove), but new features favor functions + hooks.

2. Creating Your First Components

Example 1: Simple Functional Component

Open your project → create a new file: src/components/Greeting.tsx

tsx

Now use it in App.tsx:

tsx

Key points:

  • Component name starts with Capital letter (Greeting, not greeting)
  • Returns JSX (or null, or a fragment)
  • Can be called like a custom HTML tag

Example 2: Class Component (for comparison – don’t use for new code)

tsx

You can still import and use it — but notice how much more code it takes.

3. Nesting Components (Parent → Child)

Components can contain other components — this is the heart of React.

Create two more files:

src/components/Header.tsx

tsx

src/components/Footer.tsx

tsx

Now nest them in App.tsx:

tsx

Visual structure:

text

4. Component Composition (passing children + building blocks)

The real power: compose complex UIs from small pieces.

Create a reusable Card component:

src/components/Card.tsx

tsx

Now use composition in App.tsx:

tsx

5. Exporting & Importing Components

Export style Syntax (in component file) Import syntax (in another file) Best for
Default export (most common) export default MyComponent; import MyComponent from ‘./MyComponent’; Main/primary component
Named export export function MyComponent() {} import { MyComponent } from ‘./MyComponent’; Multiple utilities/components
Named + default export default MyComponent; export function Helper() {} import MyComp, { Helper } from ‘./file’; Mixed usage

Common mistake to avoid:

tsx

Summary – Chapter 3 Key Takeaways

  • Prefer functional components + hooks in 2026 (cleaner, modern, future-proof)
  • Class components → only for error boundaries or legacy code
  • Components = functions returning JSX
  • Capital letter for component names
  • Nesting = putting components inside others
  • Composition = building complex UIs from small reusable pieces (children prop is powerful)
  • Use default exports for main components

Common Mistakes Section

  • Writing component name in lowercase → React thinks it’s HTML tag
  • Forgetting to export default → “Module not found” error
  • Putting statements (if, for) directly in JSX → use ternary / && instead
  • Mutating props inside component → props are read-only

Homework Suggestion

  1. Create 3–4 small components:
    • ProfileCard (with name, photo placeholder, bio)
    • SkillList (takes array of skills via props)
    • Button (custom button with color prop)
  2. Compose them inside a new ProfilePage component
  3. Import and use ProfilePage in App.tsx
  4. Try both default and named exports in one file and import them correctly

Let me know:

  • Any part unclear?
  • Want to see props next (Chapter 4)?
  • Want help debugging your homework?

You’re making excellent progress — keep going!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *