Course contentsShow
← React
Lesson 1600 of 2,39639. Server Components in PracticeFree lesson

Calling Server Actions from Client Components

Client Components receive Server Actions as props or imports, calling them like regular async functions with automatic serialization.

Calling Server Actions from Client Components

You'll learn how Client Components can invoke server-side logic by calling Server Actions as regular async functions.

The Bridge Between Client and Server

While Client Components run in the browser and need the 'use client' directive, they can still trigger server-side code through Server Actions. The magic? You pass Server Actions to Client Components either as props or by importing them directly, then call them like any other async function. React handles all the serialization and network communication behind the scenes.

How It Works

When you call a Server Action from a Client Component, React automatically:

  • Serializes the function arguments
  • Sends them to the server over the network
  • Executes the server-side code
  • Returns the result back to your component

Think of it like making a phone call: you speak into your phone (call the function), the phone network handles the transmission (React's serialization), and you hear the response back (awaited result).

// serverActions.ts
'use server'
export async function saveData(formData: FormData) {
  // runs on server
  const name = formData.get('name')
  await db.users.create({ name })
}

// ClientForm.tsx
'use client'
import { saveData } from './serverActions'

export function ClientForm() {
  async function handleSubmit(formData: FormData) {
    await saveData(formData) // Just call it!
    alert('Saved!')
  }

  return (
    <form action={handleSubmit}>
      <input name="name" />
      <button>Save</button>
    </form>
  )
}

No API routes, no fetch calls, no manual serialization—just call the function.

Key Takeaway: Client Components invoke Server Actions like regular async functions, with React automatically handling all the network communication and serialization between browser and server.