Next.js Discord

Discord Forum

doubt in server actions

Answered
Long-horned bees posted this in #help-forum
Open in Discord
Long-horned beesOP
I’m making CRUD operations for my Supabase database using functions in server actions and invoking them in client components. Is this the right approach, or am I doing something wrong?
Answered by Plague
Calling server actions from client component is fine as long as you follow the correct principles - https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations#client-components

Calling a server action from a Route Handler makes no sense and is a bad practice as Server Actions are also Route Handlers under the hood, so you are calling a route handler in a route handler.

They both run on the server so whatever you do in the server action do it in the route handler or extract into a regular function that is called in both places, your choice.
View full answer

5 Replies

Long-horned beesOP
I also sometimes call a server action in route handler to get data as a response. Is this a good approach?
Calling server actions from client component is fine as long as you follow the correct principles - https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations#client-components

Calling a server action from a Route Handler makes no sense and is a bad practice as Server Actions are also Route Handlers under the hood, so you are calling a route handler in a route handler.

They both run on the server so whatever you do in the server action do it in the route handler or extract into a regular function that is called in both places, your choice.
Answer
@Long-horned bees
Long-horned beesOP
Calling a server action from a Route Handler makes no sense
exactly understood that mistake! thanks for pointing it