Next.js Discord

Discord Forum

Server Actions vs. trpc

Unanswered
French Angora posted this in #help-forum
Open in Discord
French AngoraOP
hey guys I'm thinking about what I should use. I wanna stick to one thing. Thinking about data fetching and stuff combining react-query with server action if needed or fetching data via a server action on the RSC and pass it down to the children. Whats the best way to fetch Data with server actions? Also on the client (except wrapping it with react query. Or is this the way to go? )

1 Reply

Greater Shearwater
I haven't used trpc so not familiar with that.

But you don't fetch data in a server action. Server actions are for POST requests like creating and updating data.

You fetch the data in a server component. Then pass it down to the client components as you need. I usually keep all my page.tsx components as server components and fetch the data needed for the route in there and then pass down to the child client components as needed. I think it's a really nice pattern.

If there are lot of child component and if some of them are server components you could also fetch inside those so you don't have to fetch data at the very top and pass down through lot of components. Depends on the project.

If it seems like prop drilling you could also fetch the data at the page component and then wrap the first child component in a context (making that component a client component) and pass the fetched data as context through it so all the child components have access to them.