Client Components in Next.js

From the Next.js cheat sheet · Server & Client Components · verified Jul 2026

Client Components

typescript
'use client' // This directive makes it a Client Component

import { useState, useEffect } from 'react'

interface ClientComponentProps {
  initialCount?: number
  serverData: string // Props from Server Component
}

export default function InteractiveCounter({ 
  initialCount = 0,
  serverData 
}: ClientComponentProps) {
  const [count, setCount] = useState(initialCount)
  const [isClient, setIsClient] = useState(false)

  // Can use React hooks
  useEffect(() => {
    setIsClient(true)
    
    // Can access browser APIs
    console.log(window.location.href)
    localStorage.setItem('count', count.toString())
  }, [count])

  // Can use event handlers
  const handleClick = () => {
    setCount(prev => prev + 1)
  }

  return (
    <div>
      <p>Server Data: {serverData}</p>
      <p>Count: {count}</p>
      <button onClick={handleClick}>Increment</button>
      
      {/* Conditional rendering for client-only features */}
      {isClient && (
        <p>Window width: {window.innerWidth}px</p>
      )}
    </div>
  )
}

// Composing Server and Client Components
// app/page.tsx (Server Component)
import ClientCounter from './ClientCounter'

export default async function Page() {
  const data = await fetchServerData()
  
  return (
    <div>
      <h1>Server Rendered Title</h1>
      {/* Pass server data to Client Component */}
      <ClientCounter serverData={data} />
    </div>
  )
}
💡 Add "use client" directive at top
⚡ Can use hooks, browser APIs, event handlers
📌 Still pre-rendered on server (SSR)
🟢 Import Server Components as children, not directly

Continue with Next.js

Save the full cheat sheet or work through every related task.

More Next.js tasks

Back to the full Next.js cheat sheet