Can I call server actions like normal functions (with onclick or on submit)
Answered
Labib posted this in #help-forum
LabibOP
I have this small confusion with server action. Every tutorial I saw always uses server actions with the form action or useFormState hook, sends the arguments as formdata and shows how it works even without javascipt enabled.
My question is is this the only recommended way? Can I just call it like a normal function and pass any arguments? Or does it create a security issue?
My question is is this the only recommended way? Can I just call it like a normal function and pass any arguments? Or does it create a security issue?
Answered by joulev
yes you can call it as ordinary functions.
as far as security is concerned, it is no different from using
as far as security is concerned, it is no different from using
action={...}3 Replies
@Labib I have this small confusion with server action. Every tutorial I saw always uses server actions with the form action or useFormState hook, sends the arguments as formdata and shows how it works even without javascipt enabled.
My question is is this the only recommended way? Can I just call it like a normal function and pass any arguments? Or does it create a security issue?
yes you can call it as ordinary functions.
as far as security is concerned, it is no different from using
as far as security is concerned, it is no different from using
action={...}Answer
LabibOP
Thank you so much!
American Crow
Here is the docs link with an example
https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations#non-form-elements
https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations#non-form-elements