--- title: Updating Data description: Learn how to mutate data using Server Functions. related: title: API Reference description: Learn more about the features mentioned in this page by reading the API Reference. links: - app/api-reference/functions/revalidatePath - app/api-reference/functions/revalidateTag - app/api-reference/functions/redirect --- You can update data in Next.js using React's [Server Functions](https://react.dev/reference/rsc/server-functions). This page will go through how you can [create](#creating-server-functions) and [invoke](#invoking-server-functions) Server Functions. ## What are Server Functions? A **Server Function** is an asynchronous function that runs on the server. They can be called from client through a network request, which is why they must be asynchronous. In an `action` or mutation context, they are also called **Server Actions**. By convention, a Server Action is an async function used with [`startTransition`](https://react.dev/reference/react/startTransition). This happens automatically when the function is: - Passed to a `
` using the `action` prop. - Passed to a ` } ``` ```jsx filename="app/ui/button.js" switcher 'use client' import { createPost } from '@/app/actions' export function Button() { return } ``` > **Good to know:** In Client Components, forms invoking Server Actions will queue submissions if JavaScript isn't loaded yet, and will be prioritized for hydration. After hydration, the browser does not refresh on form submission. ### Passing actions as props You can also pass an action to a Client Component as a prop: ```jsx ``` ```tsx filename="app/client-component.tsx" switcher 'use client' export default function ClientComponent({ updateItemAction, }: { updateItemAction: (formData: FormData) => void }) { return {/* ... */} } ``` ```jsx filename="app/client-component.js" switcher 'use client' export default function ClientComponent({ updateItemAction }) { return
{/* ... */}
} ``` ## Invoking Server Functions There are two main ways you can invoke a Server Function: 1. [Forms](#forms) in Server and Client Components 2. [Event Handlers](#event-handlers) and [useEffect](#useeffect) in Client Components ### Forms React extends the HTML [`
`](https://react.dev/reference/react-dom/components/form) element to allow Server Function to be invoked with the HTML `action` prop. When invoked in a form, the function automatically receives the [`FormData`](https://developer.mozilla.org/docs/Web/API/FormData/FormData) object. You can extract the data using the native [`FormData` methods](https://developer.mozilla.org/en-US/docs/Web/API/FormData#instance_methods): ```tsx filename="app/ui/form.tsx" switcher import { createPost } from '@/app/actions' export function Form() { return (
) } ``` ```jsx filename="app/ui/form.js" switcher import { createPost } from '@/app/actions' export function Form() { return (
) } ``` ```ts filename="app/actions.ts" switcher 'use server' export async function createPost(formData: FormData) { const title = formData.get('title') const content = formData.get('content') // Update data // Revalidate cache } ``` ```js filename="app/actions.js" switcher 'use server' export async function createPost(formData) { const title = formData.get('title') const content = formData.get('content') // Update data // Revalidate cache } ``` ### Event Handlers You can invoke a Server Function in a Client Component by using event handlers such as `onClick`. ```tsx filename="app/like-button.tsx" switcher 'use client' import { incrementLike } from './actions' import { useState } from 'react' export default function LikeButton({ initialLikes }: { initialLikes: number }) { const [likes, setLikes] = useState(initialLikes) return ( <>

Total Likes: {likes}

) } ``` ```jsx filename="app/like-button.js" switcher 'use client' import { incrementLike } from './actions' import { useState } from 'react' export default function LikeButton({ initialLikes }) { const [likes, setLikes] = useState(initialLikes) return ( <>

Total Likes: {likes}

) } ``` ## Examples ### Showing a pending state While executing a Server Function, you can show a loading indicator with React's [`useActionState`](https://react.dev/reference/react/useActionState) hook. This hook returns a `pending` boolean: ```tsx filename="app/ui/button.tsx" switcher 'use client' import { useActionState, startTransition } from 'react' import { createPost } from '@/app/actions' import { LoadingSpinner } from '@/app/ui/loading-spinner' export function Button() { const [state, action, pending] = useActionState(createPost, false) return ( ) } ``` ```jsx filename="app/ui/button.js" switcher 'use client' import { useActionState, startTransition } from 'react' import { createPost } from '@/app/actions' import { LoadingSpinner } from '@/app/ui/loading-spinner' export function Button() { const [state, action, pending] = useActionState(createPost, false) return ( ) } ``` ### Revalidating After performing an update, you can revalidate the Next.js cache and show the updated data by calling [`revalidatePath`](/docs/app/api-reference/functions/revalidatePath) or [`revalidateTag`](/docs/app/api-reference/functions/revalidateTag) within the Server Function: ```ts filename="app/lib/actions.ts" switcher import { revalidatePath } from 'next/cache' export async function createPost(formData: FormData) { 'use server' // Update data // ... revalidatePath('/posts') } ``` ```js filename="app/actions.js" switcher import { revalidatePath } from 'next/cache' export async function createPost(formData) { 'use server' // Update data // ... revalidatePath('/posts') } ``` ### Redirecting You may want to redirect the user to a different page after performing an update. You can do this by calling [`redirect`](/docs/app/api-reference/functions/redirect) within the Server Function: ```ts filename="app/lib/actions.ts" switcher 'use server' import { redirect } from 'next/navigation' export async function createPost(formData: FormData) { // Update data // ... redirect('/posts') } ``` ```js filename="app/actions.js" switcher 'use server' import { redirect } from 'next/navigation' export async function createPost(formData) { // Update data // ... redirect('/posts') } ``` ### Cookies You can `get`, `set`, and `delete` cookies inside a Server Action using the [`cookies`](/docs/app/api-reference/functions/cookies) API: ```ts filename="app/actions.ts" switcher 'use server' import { cookies } from 'next/headers' export async function exampleAction() { const cookieStore = await cookies() // Get cookie cookieStore.get('name')?.value // Set cookie cookieStore.set('name', 'Delba') // Delete cookie cookieStore.delete('name') } ``` ```js filename="app/actions.js" switcher 'use server' import { cookies } from 'next/headers' export async function exampleAction() { // Get cookie const cookieStore = await cookies() // Get cookie cookieStore.get('name')?.value // Set cookie cookieStore.set('name', 'Delba') // Delete cookie cookieStore.delete('name') } ``` ### useEffect You can use the React [`useEffect`](https://react.dev/reference/react/useEffect) hook to invoke a Server Action when the component mounts or a dependency changes. This is useful for mutations that depend on global events or need to be triggered automatically. For example, `onKeyDown` for app shortcuts, an intersection observer hook for infinite scrolling, or when the component mounts to update a view count: ```tsx filename="app/view-count.tsx" switcher 'use client' import { incrementViews } from './actions' import { useState, useEffect, useTransition } from 'react' export default function ViewCount({ initialViews }: { initialViews: number }) { const [views, setViews] = useState(initialViews) const [isPending, startTransition] = useTransition() useEffect(() => { startTransition(async () => { const updatedViews = await incrementViews() setViews(updatedViews) }) }, []) // You can use `isPending` to give users feedback return

Total Views: {views}

} ``` ```jsx filename="app/view-count.js" switcher 'use client' import { incrementViews } from './actions' import { useState, useEffect, useTransition } from 'react' export default function ViewCount({ initialViews }) { const [views, setViews] = useState(initialViews) const [isPending, startTransition] = useTransition() useEffect(() => { startTransition(async () => { const updatedViews = await incrementViews() setViews(updatedViews) }) }, []) // You can use `isPending` to give users feedback return

Total Views: {views}

} ```