Next.js Discord

Discord Forum

Can Server Actions return data to either a Client Component/Server Component

Unanswered
European Storm-Petrel posted this in #help-forum
Open in Discord
European Storm-PetrelOP
Forgive me if it's a simple question but I need some interactivity.

I have a server component page
It as a button (can be server/client I'm not sure what's appropiate)
Upon clicking it will fetch some data.
I want to render that data once the fetch is done.

What would be the best strategy here?

4 Replies

I think with useActionState you can return the data and use it on the component to render the data
buttons with an onClick handler will need to be client components. you can trigger the action and have a stream be set up in a server component and you can push that stream to it, kinda like an ai chatbot implementation. however, i am not sure you need that.

I'd just do the fetch on the client, set the data to React state and display that state in the component
@European Storm-Petrel Forgive me if it's a simple question but I need some interactivity. I have a server component page It as a button (can be server/client I'm not sure what's appropiate) Upon clicking it will fetch some data. I want to render that data once the fetch is done. What would be the best strategy here?
It's recommended to use some sort of client-side data fetching libary like React Query or SWR, but if you don't want to use those, you can just call the Server Action inside the onClick handler and then update local state with that data