Server Action in Client and Server Component in Next.Js Explained
Feb 25, 2024
0
Server Actions are asynchronous functions that are executed on the server. They can be used in Server and Client Components to handle form submissions and data mutations in Next.js applications.
We can use Server Actions in client and server both type of components. Server Actions acts as functions which makes them reusable across app. All type of server actions send a POST request to server and only this HTTP methods can invoke these functions.
There are several way we can use server action as per our need and with support of useFormStatus
, useOptimistic
and useTransition
hooks it makes so easy to handle all the edge cases.
Server Component#
As name suggest Server Actions using in server component is easy and straight forward but since it's server component come with few limitations too when it's come to error handling which we can do but not that elegant way we do in client component.
For example we wanna add formdata in our DB here is the example how we can do in server component.
Here as you can see we are using use server
comment to tell next.js that this is server action and it will send a POST request to server. Since ServerExample
component is server component we can use actions in component direct just have to tell next.js that this is server action using use server
comment.
In server action we get the data and we can do our DB work and if any error happen we can throw error and it will be handled by error boundary or error.ts
file.
We are using ZOD
for schema validation which is a great library for schema validation.
Now since we want to get the Loading
state we can use useFormStatus
hook which give us the loading state for that we have created a client component which is our Button component.
useFormStatus hook gives us the pending
state and we can use it to show the loading state. we can use this component for client and server both type of server actions.
Error Boundary#
Next.js has a great feature of error boundary which we can use to handle the error in server component. We can create a file error.ts
in page root folder and it will be used to handle the error in server component.
Error component must need to be client component and it will be used to handle the error in server component.
Neon DB with Drizzle and Hono in Next.JS
Hono is lightweight, small, simple, and ultrafast web framework for the Edges. Hono makes easy to create Rest API in Next.js app. While Drizzle is Typescript ORM which support edges network out of the box.
Read Full PostClient Component#
Server actions with client component are so easy and straight forward. We can use server actions in client component and it will send a POST request to server.
Clients component gives us flexibility to handle the error and loading state in more elegant way.
Example component is client component and we can get the benefit of React hooks and here we are using useRef
to get the form reference and after successful form submission we are resetting the form.
We can use any client side error handling library like react-toastify
or react-alert
to show the error message. or with state we can show the error message.
Since Example
component is client component we can't use server action directly in component. We can create a file actions.ts
and then we can use server action in that file.
Here we are using use server
comment to tell next.js that this is server action and must run on server.
We can use ZOD
for schema validation and then we can do our DB work and if any error we can throw error and it will be handled by client component.
Server Actions and useOptimistic#
useOptimistic hook is used to handle the optimistic UI updates. It is used to update the UI before the server response. It is used to give the user a better experience.
Let's we want functionality to add a Like to a post and we want to show the like count instantly and then we will send a request to server to update the like count. and if any error we will revert the UI to previous state.
Here is the example how we can use useOptimistic hook with server action.
Here we are using useOptimistic
hook to update the UI before the server response. while to use useOptimistic
hook we need to use startTransition
to wrap it.
We are then sending a request to server to update the like count and if any error we will revert the UI to previous state.
As usual we are ZOD
from schema validation and then we can do our DB work and if any error we can throw error and it will be handled by client component.
Also we are using revalidatePath
to revalidate the data in cache for current path.
Server Actions more ways to use#
We can use Server Actions in non-form Elements too. We can use in any event handler like onClick
, onMouseOver
, onMouseOut
etc.
Here is the example how we can use Server Actions in non-form Elements.
We can use Server Actions in useEffect
hook too, If we want to update page views when component mount.
Conclusion#
With the rise of RSC(React server components) Server Actions are going to be more powerful and It's a great way to handle form submissions and data mutations in Next.js applications.
We now don't need to worry about the server and client component since we can use server actions in both type of components and easy to get started and not much boilerplate code.
I hope this article will help you to understand the Server Actions in Next.js and how to use them in client and server components. If you have any question or suggestion feel free to ask in the comment section below. You can find the complete code on GitHub.
Comments (2)