#server actions question

8 messages · Page 1 of 1 (latest)

hidden salmon
#

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?

worn zincBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

      🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in <id:customize>

      ✅ You can mark a message as the answer for your post with `Right click -> Apps -> Mark Solution`
      (if you don't see the option, try refreshing Discord with Ctrl + R)
drowsy thorn
#

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>
        </>
    )
golden rivet
#

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.

drowsy thorn
#

i just answered OP doubt