DEV Community

Rajae Robinson
Rajae Robinson

Posted on

Simplifying Next.js Server Actions

Hey everyone,

I wrote an article about Next.js Server Actions. This new feature provides powerful data-fetching capabilities in Next.js applications and leverages React Server Components to optimize performance while keeping bundle sizes minimal.

Understanding React Server Components

React Server Components (RSC) represent a significant shift in web development. They move components to the server and align them with data stores, resulting in improved app performance without increasing bundle size.

NOTE: In Next.js 13, components in the /app directory are server components by default. To designate a component as a client component, use the "use client" directive.

'use client'
Enter fullscreen mode Exit fullscreen mode

Demystifying Server Actions

Server Actions in Next.js introduce powerful data fetching techniques. They are asynchronous JavaScript functions that execute on the server in response to user interactions on the client.

// Example of a Server Action
async function addTodoItem(data) { 
    'use server'; 
    await addTodoItem(data); 
}
Enter fullscreen mode Exit fullscreen mode

These actions can be triggered using the action prop of a <form> element in React:

<form action={addTodoItem}> 
    {/* Form fields */} 
</form>
Enter fullscreen mode Exit fullscreen mode

Implementing Server Actions

You can integrate server actions within server components by creating an async function with the "use server" directive at the top.

export default function TodoList() {
  async function addTodoItem(todoId, description) {
    'use server';
    await saveTodo({ todoId, description });
  }

  return (
    <form action={addTodoItem}>
      <input type="text" id="todo" name="todo" />
      <button type="submit">Add Todo</button>
    </form>
  );
}
Enter fullscreen mode Exit fullscreen mode

Using Server Actions in Client Components

In scenarios where both server and client components coexist, you may want to employ server actions within client components. Create your actions in a separate file and ensure the "use server" directive is at the top. For instance:

'use server'

export async function addTodoItem(data) {
  await addTodoItem(data);
}
Enter fullscreen mode Exit fullscreen mode

Revalidating Data with Server Actions

Server Actions integrate seamlessly with caching and revalidation. This allows for multiple actions per route, optimizing performance and responsiveness.

// Example of revalidating data
'use server'

import { revalidatePath } from 'next/cache';

export default async function submit() {
  await updateUserInfo();
  revalidatePath('/profile'); // Revalidate user profile data
}
Enter fullscreen mode Exit fullscreen mode

The article also covers invoking Server Actions outside of forms and redirecting with Server Actions, providing a comprehensive guide to making the most out of Next.js Server Actions.

You can read the full article here. You may also find more beginner-friendly posts on Next.js, React.js, and Typescript at bluesockets.com.

Have any of you used Next.js Server Actions before? What are your thoughts?

Looking forward to hearing your insights!

Top comments (0)