Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    NNext.js

    Next.js foundations

    • What is Next.js?
    • The App Router: folders are routes
    • Dynamic routes
    • Client-side navigation

    Server, client and data

    • Server and Client Components
    • Fetching data on the server
    • loading, error and not-found
    • Caching and revalidation

    Mutations, APIs and security

    • Server Actions
    • Route Handlers: building an API
    • Proxy (formerly Middleware)
    • Authentication and protecting pages

    Going to production

    • Metadata and SEO
    • Images, fonts and performance
    • Environment variables and deploying to Vercel
    • Project: a course catalogue
    Next.js

    Server, client and data · Lesson 5 of 16

    Server and Client Components

    Which code runs where, "use client", and the boundary between them.

    11 minutes2 games

    In the App Router every component is a Server Component by default: it runs only on the server, and the browser receives the resulting HTML without its JavaScript. You can query the database directly, use secret keys and heavy libraries, and none of it reaches the user.

    But Server Components have no interactivity: no useState, onClick or useEffect. For those, write "use client" at the top of a file, making that component (and everything it imports) a Client Component.

    TypeScript
    // app/courses/[slug]/page.tsx: a Server Component (the default)
    import { db } from "@/lib/db"              // server-only code, never sent to the browser
    import { LikeButton } from "./like-button"
    
    export default async function CoursePage({ params }: { params: Promise<{ slug: string }> }) {
      const { slug } = await params
      const course = await db.course.findBySlug(slug)      // direct database access
      return (
        <article>
          <h1>{course.title}</h1>
          <p>{course.description}</p>
          <LikeButton courseId={course.id} initialLikes={course.likes} />
        </article>
      )
    }
    
    // app/courses/[slug]/like-button.tsx: a Client Component
    "use client"
    import { useState } from "react"
    
    export function LikeButton({ courseId, initialLikes }: { courseId: string; initialLikes: number }) {
      const [likes, setLikes] = useState(initialLikes)
      return <button onClick={() => setLikes(likes + 1)}>♥ {likes}</button>
    }
    • CoursePage (server): reads the DB, sends HTML only
      • <h1>, <p> (server): zero JavaScript
      • "use client" boundary
        • LikeButton (client): HTML + its JavaScript, hydrated in the browser

    Only client components' JavaScript reaches the browser.

    Important

    Props passed from Server to Client must be serializable: strings, numbers, arrays, plain objects, Dates. You can't pass an ordinary function, a class instance or a database connection. The exception: server actions ("use server" functions) can be passed.

    NeedServerClient

    fetch data from the database

    ✅

    ❌

    secret API keys

    ✅

    ❌

    useState / useEffect

    ❌

    ✅

    onClick, onChange

    ❌

    ✅

    window, localStorage

    ❌

    ✅

    less browser JavaScript

    ✅

    ❌

    Put "use client" on the smallest possible component (the button, not the whole page). You can also pass Server Components as children into a Client Component, and they stay server-rendered.

    Spot the bug

    This file errors. Which line is the cause?

    Interview question

    What are React Server Components, and how do they differ from SSR?

    Exercise

    This LikeButton is a client component: it takes initialLikes from the server, each press adds one, and it shows ♥ N. The checker presses twice, starting from 41.

    The page should show: ♥ 43

    React
    function LikeButton({ initialLikes }) {
      return <button>♥ {initialLikes}</button>
    }
    
    // Stands in for the Server Component that passes data down
    function App() {
      return <LikeButton initialLikes={41} />
    }

    Learn more from

    • Next.js docs: Server and Client Componentsnextjs.org
    • react.dev: Server Componentsreact.dev
    • react.dev: 'use client'react.dev
    • Vercel blog: Understanding React Server Componentsvercel.com

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousClient-side navigationNextFetching data on the server
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.