server actions question
Unanswered
Southeastern blueberry bee posted this in #help-forum
Southeastern blueberry beeOP
As I understand it, there are two ways of using server actions: with server components and/or with client components. But how do server actions work in server components when event handlers are only supposed to run on the client side?
7 Replies
it does not work actually if it touch window or something. event handlers delegate to server actions with RPC over HTTP.
keep in mind that server action codes reside in server side, not downloaded into web browser.
but you can invoke it as if it is local func, ie. RPC. in the example below, addItem() implementation is not in downloaded js codes, thus you can call DB or private APIs with accessToken.
const handler = async (p: string) => {
await addItem(p) // Server Action
}
return (
<>
<button onClick={() => startTransition(() => handler(param1))}>
Add To Cart
</button>
</>
)The above info is only relevant in client components which wasnt your question
the default way it works is because it is using <form> as an action handler that sends a POST, and forms are not bound to a window-required event handler for action.
the default way it works is because it is using <form> as an action handler that sends a POST, and forms are not bound to a window-required event handler for action.
i just answered OP doubt
OP is mixed up event handlers with SA.