react-native, nextjs and server actions
Answered
René Goretzka posted this in #help-forum
Hey everyone,
I want to create a web and mobile application, but I do not want to recreate the API layer and eventually use server actions for mutations and an endpoint to do queries in my react-native application.
What would be the proper way to achieve that? I am guessing that it is currently not working with next.js and might have to use tRPC to get to a similar result? I would probably not use server actions anymore, but still be able to do SSR with tRPC and Server Components.
I would appreciate feedback and pointers.
Thank you!
I want to create a web and mobile application, but I do not want to recreate the API layer and eventually use server actions for mutations and an endpoint to do queries in my react-native application.
What would be the proper way to achieve that? I am guessing that it is currently not working with next.js and might have to use tRPC to get to a similar result? I would probably not use server actions anymore, but still be able to do SSR with tRPC and Server Components.
I would appreciate feedback and pointers.

Thank you!
Answered by joulev
if you use server components to fetch data, you have to use server actions. it is the only thing that can revalidate your data instantly – running
you should make both server actions and an api layer, the server actions for your web frontend while the api layer for other applications.
revalidatePath/revalidateTag outside server actions will require a page refresh.you should make both server actions and an api layer, the server actions for your web frontend while the api layer for other applications.
6 Replies
@René Goretzka Hey everyone,
I want to create a web and mobile application, but I do not want to recreate the API layer and eventually use server actions for mutations and an endpoint to do queries in my react-native application.
What would be the proper way to achieve that? I am guessing that it is currently not working with next.js and might have to use tRPC to get to a similar result? I would probably not use server actions anymore, but still be able to do SSR with tRPC and Server Components.
I would appreciate feedback and pointers. <:amazing:753872185084346468>
Thank you!
if you use server components to fetch data, you have to use server actions. it is the only thing that can revalidate your data instantly – running
you should make both server actions and an api layer, the server actions for your web frontend while the api layer for other applications.
revalidatePath/revalidateTag outside server actions will require a page refresh.you should make both server actions and an api layer, the server actions for your web frontend while the api layer for other applications.
Answer
Hmmm... yes, I would still use server-actions, but those would use the tRPC to mutate data. Does that make sense? That way I only have to maintain the tRPC and proxy from the server-actions for the web app.
@René Goretzka Hmmm... yes, I would still use server-actions, but those would use the tRPC to mutate data. Does that make sense? That way I only have to maintain the tRPC and proxy from the server-actions for the web app.
the server actions are already run on the server. so use server-side logic there directly. trpc is for fetching/mutating data from the browser
How can I prevent duplication of code then? Is there no better way?
the main logic, put them in reusable functions. import and use those functions in server actions and trpc routes
Hmmmm... yeah that makes sense. Thank you joulev!