Component Architecture

Loading concept...

๐Ÿ  Next.js Component Architecture: The Smart House Story

Imagine youโ€™re building a magical house where different rooms have different superpowers. Some rooms work automatically (you donโ€™t even need to be there!), while others only work when YOU are inside and pressing buttons.

This is exactly how Next.js Component Architecture works!


๐ŸŒŸ The Big Picture

graph TD A[๐Ÿ  Your Next.js App] --> B[๐Ÿค– Server Components] A --> C[๐Ÿ‘† Client Components] B --> D[Fast & Smart] C --> E[Interactive & Fun]

Think of your app as a house with two types of rooms:

  • Server Rooms ๐Ÿค– = Work automatically, even when no oneโ€™s home
  • Client Rooms ๐Ÿ‘† = Need YOU to press buttons and interact

๐Ÿค– Server Components

What Are They?

Server Components are like robot helpers that prepare everything BEFORE you arrive home.

Imagine coming home and:

  • ๐Ÿฝ๏ธ Dinner is already cooked
  • ๐Ÿ“š Your books are already on the desk
  • ๐Ÿ–ผ๏ธ Pictures are already on the wall

The robot did all the work on the โ€œserverโ€ (the kitchen in the back) โ€” you just enjoy the result!

Why Are They Amazing?

Superpower What It Means
โšก Super Fast No waiting! Everythingโ€™s ready
๐Ÿ“ฆ Smaller Package Less stuff sent to your phone
๐Ÿ”’ Safe Secrets Can talk to databases directly

Simple Example

// This is a Server Component!
// It runs on the server (the kitchen)

async function WelcomeMessage() {
  // Can fetch data directly!
  const user = await getUser();

  return (
    <h1>Hello, {user.name}!</h1>
  );
}

Key Point: In Next.js, components are Server Components by default. You donโ€™t need to do anything special!


๐Ÿ‘† Client Components

What Are They?

Client Components are like light switches โ€” they only work when YOU touch them!

Think about:

  • ๐Ÿ’ก Flipping a light switch
  • ๐ŸŽฎ Playing a video game
  • ๐Ÿ“ฑ Tapping buttons on your phone

These need YOUR action to work. Thatโ€™s Client Components!

When Do You Need Them?

graph TD A[Does it need...?] --> B[Clicking buttons?] A --> C[Typing in forms?] A --> D[Animations?] A --> E[Browser features?] B --> F[โœ… Use Client Component] C --> F D --> F E --> F

Simple Example

'use client'  // ๐Ÿ‘ˆ Magic words!

import { useState } from 'react';

function LikeButton() {
  const [likes, setLikes] = useState(0);

  return (
    <button onClick={() => setLikes(likes + 1)}>
      โค๏ธ {likes} Likes
    </button>
  );
}

๐Ÿ“œ The 'use client' Directive

The Magic Words

Think of 'use client' as a magic spell at the top of your file.

It tells Next.js: โ€œHey! This room needs people inside to work!โ€

Rules to Remember

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  'use client'                       โ”‚
โ”‚                                     โ”‚
โ”‚  โœ… Must be at the VERY TOP         โ”‚
โ”‚  โœ… Before any imports              โ”‚
โ”‚  โœ… Must have quotes around it      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Correct Way

'use client'  // โœ… First line!

import { useState } from 'react';
import Button from './Button';

function MyComponent() {
  // Now I can use useState, onClick, etc!
}

Wrong Way

import { useState } from 'react';
'use client'  // โŒ Too late!

function MyComponent() {
  // This will cause errors!
}

๐Ÿ”ง The 'use server' Directive

Server Actions = Sending Messages to the Kitchen

Imagine youโ€™re in the living room (client) and want to order food. You send a message to the kitchen (server) saying โ€œMake me a sandwich!โ€

Thatโ€™s what 'use server' does!

Simple Example

'use server'

async function saveToDatabase(data) {
  // This runs on the server!
  // Safe to use database here
  await db.save(data);
}

Using It In a Client Component

'use client'

import { saveData } from './actions';

function ContactForm() {
  async function handleSubmit(formData) {
    await saveData(formData); // Sends to server!
  }

  return (
    <form action={handleSubmit}>
      <input name="email" />
      <button>Send</button>
    </form>
  );
}
graph LR A[๐Ÿ‘† Client] -->|sends request| B[๐Ÿค– Server] B -->|does the work| C[#40;Database#41;] B -->|sends result| A

๐ŸŽ Server to Client Patterns

The Gift-Wrapping Pattern

Think of it like this:

  • ๐ŸŽ Server prepares the gift (data)
  • ๐Ÿ“ฆ Server wraps it nicely (component)
  • ๐Ÿšš Server sends it to the Client
  • ๐Ÿ‘† Client just unwraps and enjoys!

Pattern 1: Passing Data as Props

// ServerParent.jsx (Server Component)
async function ServerParent() {
  const data = await fetchData(); // Heavy work

  return (
    <ClientChild data={data} />
  );
}
// ClientChild.jsx (Client Component)
'use client'

function ClientChild({ data }) {
  const [show, setShow] = useState(false);

  return (
    <div onClick={() => setShow(!show)}>
      {data.title}
    </div>
  );
}

Pattern 2: Children Pattern

// Layout.jsx (Server Component)
async function Layout({ children }) {
  const user = await getUser();

  return (
    <div>
      <Header user={user} />
      {children}  {/* ๐Ÿ‘ˆ Can be Client! */}
    </div>
  );
}

Visual Summary

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  ๐Ÿค– Server Component         โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚ ๐Ÿ‘† Client Component    โ”‚  โ”‚
โ”‚  โ”‚    (as children)       โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Golden Rule: Server wraps Client, not the other way around!


๐ŸŒ Context Providers Setup

The Announcement System

Imagine your house has a speaker system. When you make an announcement, EVERY room hears it!

Thatโ€™s what Context Providers do โ€” they share information with ALL components below them.

The Challenge

Context Providers need 'use client' because they use React features. But we want them at the TOP of our app!

The Solution: Create a Wrapper

Step 1: Create a Provider Component

// providers.jsx
'use client'

import { ThemeProvider } from 'next-themes';
import { AuthProvider } from './auth';

export function Providers({ children }) {
  return (
    <ThemeProvider>
      <AuthProvider>
        {children}
      </AuthProvider>
    </ThemeProvider>
  );
}

Step 2: Use It in Layout

// layout.jsx (Server Component!)
import { Providers } from './providers';

export default function RootLayout({ children }) {
  return (
    <html>
      <body>
        <Providers>
          {children}
        </Providers>
      </body>
    </html>
  );
}

How It Works

graph TD A[๐Ÿค– Layout - Server] --> B[๐Ÿ‘† Providers - Client] B --> C[๐ŸŽจ ThemeProvider] B --> D[๐Ÿ” AuthProvider] C --> E[Your App Components] D --> E

Magic Result: Your layout stays a Server Component, but all children can access the providers!


๐ŸŽฏ Quick Decision Guide

Should this be a Client Component?

Question If YES If NO
Uses useState or useEffect? Client Server
Has onClick or onChange? Client Server
Uses browser APIs? Client Server
Just displays data? Server Server
Fetches from database? Server Server

๐Ÿ† Remember These Rules!

  1. Default = Server

    • Components are Server by default. Yay!
  2. 'use client' = Interactive

    • Add this for buttons, forms, and fun stuff
  3. 'use server' = Actions

    • For functions that talk to databases
  4. Server Wraps Client

    • Never put Server inside Client
  5. Providers Need Wrapping

    • Create a client wrapper for context

๐ŸŽ‰ You Did It!

You now understand the Next.js Component Architecture!

Think of it like this:

  • ๐Ÿค– Server Components = Robot helpers preparing things
  • ๐Ÿ‘† Client Components = Interactive buttons you press
  • ๐Ÿ“œ Directives = Magic spells that say whatโ€™s what
  • ๐ŸŽ Patterns = Smart ways to combine them
  • ๐ŸŒ Providers = The announcement system for everyone

Youโ€™re ready to build amazing apps! ๐Ÿš€

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.