Lee contrasts Pages Router forms, which use an onSubmit handler and a separate API route, with App Router forms. The example is a todo list where users can add and delete items, with loading and error states.
Using Forms in Next.js
Speaker
Lee, a software engineer at Vercel, explains how to build forms with Server Actions and revalidate data in the Next.js App Router.
The page is an asynchronous Server Component that fetches todos, in this example from Vercel Postgres. A form’s action calls a server-side createTodo function, which validates input with Zod, inserts the todo, and calls revalidatePath so the page can show updated data.
A submit button uses React’s useFormStatus hook to show when a form is pending. Delete forms can be rendered for each todo and pass its ID in hidden form data. Lee notes that a Server Action can update cached data and return new UI in one network round trip.
The example labels inputs for screen readers and uses a polite live area to announce successful or failed actions. Lee demonstrates an error message after a two-second delay and explains why the button uses aria-disabled instead of disabled in this example.
For finer control than revalidating an entire path, Lee describes using revalidateTag with tagged data. He also mentions upcoming support for tagging data accessed through cache(), and names cookies, headers, optimistic UI, and redirects as other Server Action use cases.
Takeaways
- Use a Server Action as a form action to handle mutations without creating a separate API route.
- Validate submitted form data on the server before writing it to a database.
- Call revalidatePath after a mutation so the page can refresh its cached data.
- Use pending state and screen reader announcements to make form feedback clearer.
- Use revalidateTag when you need to refresh specific tagged data instead of a whole path.
Mentioned in this talk
Search inside this talk
What was claimed 8
- feature
Next.js: Server Actions can process forms by calling server-side functions without a separate API route.
Server Actions can process forms by calling server-side functions without a separate API route.“you can mutate your data directly”
2:16inVercel news, React Canaries & RN macOS 0.71YouTube Verified2 sources· Talk
- usesServer Actions uses Zod
“We're using Zod to validate that the data we're getting back is correct”
YouTube Unverified1 source· Talk
- uses
“Then we revalidate the data.”
YouTube Unverified1 source· Talk
- usesServer Actions uses
Postgres
“we call Vercel Postgres, or our Postgres database, to insert this new value”
YouTube Unverified1 source· Talk
- featureProgressive enhancement: The demonstrated forms work with JavaScript enabled or disabled.
“This works whether we have JavaScript enabled or disabled”
YouTube Unverified1 source· Talk
- usesuseFormStatus uses React
“It reads from a new React hook called useFormStatus.”
YouTube Unverified1 source· Talk
- usesuseFormState uses React
“useFormStatus and useFormState, are coming from React.”
YouTube Unverified1 source· Talk
- featureApp Router: Server Action responses can update cached data and provide new UI in one network round trip.
“data returned from the server actions can not only update the cached data, but also provide the new UI in one network round trip”
YouTube Unverified1 source· Talk
Related talks
25:03
12:33
23:57
4:21
9:12
14:29