Next.js Discord

Discord Forum

Best way to handle server action in next js

Answered
American posted this in #help-forum
Open in Discord
AmericanOP
what's the best way to handle Next.js Server Actions to track pending states and trigger post-completion tasks (e.g., redirects)? I’m considering TanStack Query and next-safe-action—which library is more beneficial for this use case, and why?

11 Replies

@American what's the best way to handle Next.js Server Actions to track pending states and trigger post-completion tasks (e.g., redirects)? I’m considering TanStack Query and next-safe-action—which library is more beneficial for this use case, and why?
I like to use next-safe-action as it provides everything that you need for server actions. React Query is more to Query data and not to mutate data. Server actions are made for serverside mutation
Dwarf Hotot
Can you give more details what action you are referring than I can give best option for every action
@Dwarf Hotot Can you give more details what action you are referring than I can give best option for every action
AmericanOP
i am building a Next.js MCQ/Checkbox Quiz Application with real-time answer persistence (no submit button).
it involves real-time CRUD operations triggered by user selections:
1 -Create/Update on answer selection
2-Delete on answer deselection
3-Read existing answers on load
my Component Structure is like
i am fetching mcq questions and answer in server component and passing to through props to child component
where i am also doing crud
so i am facing problem like for example i have selected an answer and i do api call for that , if that fails , that answer should be unselected and previous answer should be selected with a error toast
also sometime i need to get latest updated data of answer which i am fetching in parent server componnet , after seleting answer , how can i make that api call again in parent component
@American i read that we an also handle server action with mutation which are provided by TanStack Query..
yea, I would strictly separate both from another. Server action shouldn't be used for fetching data and for me RQ shouldn't be used for mutations on the server
@B33fb0n3 yea, I would strictly separate both from another. Server action shouldn't be used for fetching data and for me RQ shouldn't be used for mutations on the server
AmericanOP
i am not fetching data with server action , i am using them only for mutation.
can you tell why we should no use tanstack mutation for server action ?
@American i am not fetching data with server action , i am using them only for mutation. can you tell why we should no use tanstack mutation for server action ?
I would strictly separate both from another. Of course it may work using RQ for mutations and server actions for fetching data, but I separate them and using both for what I think they work best
@American solved?
@B33fb0n3 <@931103240341319701> solved?
AmericanOP
yeah for mutation i am using next-safe-action and if any other api is affected by the mutation i am calling ravalidatePath() or revalidateTag() to fetch latest data after mutation
Answer