#Does server actions work when used in onClick (as in React's documentation)?

18 messages · Page 1 of 1 (latest)

wicked phoenix
#

I've tried to use server action the same as in https://19.react.dev/reference/rsc/server-actions#creating-a-server-action-from-a-server-component but always get this error Error: Only plain objects, and a few built-ins, can be passed to Server Actions. Classes or null prototypes are not supported.

It works when used in action prop but not in onClick - here's repo with both cases https://dub.sh/JYXdozO

GitHub

Contribute to arekbartnik/server-actions-test development by creating an account on GitHub.

The library for web and native user interfaces

lost lionBOT
#

🔎 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)

oak cypress
# wicked phoenix I've tried to use server action the same as in https://19.react.dev/reference/rs...

it looks like your component is not declared as client component. onClick-Events are only useable in client component. So, I guess you want to have it as server component. That's fine and you defined your server action also correctly.

However when it comes to client side interactivity the server action also need to be defined for that. You can see it here: https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations#client-components
So create a actions.ts and insert your function there.

Like that you are able to use your function in your action-prop as well as in the click event handler (make sure you move your button in another component to mark this other component as client).

wicked phoenix
#

I know I could define server action in separate file with top level "use server" but React's documentation specify that I should be able to use it inline - I would like to know if it something not implemented in NextJs or if I'm doing something wrong...

oak cypress
wicked phoenix
#

if this is something that is different about server actions in NextJs than “vanilla” React I would like some confirmation in the NextJs docs 🤔

oak cypress
wicked phoenix
#

according to the React docs I should be able to use onClick with server actions in server components - so NextJs didn't implement it?

stuck coral
#

reason: onClick={addUser} is equivalent to onClick={(...manyThings) => addUser(...manyThings)}, this manyThings is not a valid data type for server action as per the error message

#

only certain data types are allowed to be passed to server actions

#

calling server action from useEffect, onClick and any client-side events is fully supported and possible.

stuck coral
wicked phoenix
#

I understand your explanation but React's documentation gives an example that it should be possible to use server action in onClick without passing it as an anonymous function <- this is the point of my confusion 😅

stuck coral
#

oh you are right, yeah the documentation is wrong there

wicked phoenix
#

mistake in docs confirmed by Ricky from React Team

oak cypress
#

@wicked phoenix solved? Please mark solution: