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

Returning Data from Server Actions

Server Actions can return serializable values that become available to the caller after the action completes.

Returning Data from Server Actions

What You'll Learn

You'll learn how Server Actions can return data back to the caller, enabling you to get results from server-side operations directly in your components.

The Core Idea

Unlike traditional form submissions that navigate to a new page, Server Actions can return serializable values that your component receives after the action completes. This means you can perform server-side operations and immediately use the results in your UI—perfect for showing success messages, updated data, or validation errors.

Think of it like calling a function that happens to run on the server: you invoke it, it does work remotely, and then it hands you back a result you can use right away.

How It Works

When you define a Server Action, you can return any serializable value:

// Server Action
async function createUser(formData: FormData) {
  'use server'
  
  const name = formData.get('name')
  // ... save to database
  
  return { success: true, userId: 123, name: name }
}

From a Client Component, you can capture that return value:

'use client'

export default function SignupForm() {
  async function handleSubmit(formData: FormData) {
    const result = await createUser(formData)
    
    if (result.success) {
      console.log(`User ${result.name} created with ID: ${result.userId}`)
    }
  }
  
  return <form action={handleSubmit}>...</form>
}

The same serialization rules apply: you can return objects, arrays, strings, numbers, but not functions or class instances.

Use Cases

This pattern is essential for showing confirmation messages, validation errors, or updated records without a full page reload.

Key Takeaway: Server Actions can return serializable data that becomes immediately available to the caller, enabling seamless server-client communication without page navigation.