#useActionState, server actions and no FormData

1 messages · Page 1 of 1 (latest)

limpid field
#

Hi everyone, I have a really complex multistep form built with shadcn, zod and react hook form. On the server, I have a server action that receives the formData; on the client, I have a useActionState with the server action. I insert the action inside <form action={action}> like always. But with this multiform steps, in the server action I receive no formData (it's empty) while on frontend the react hook form has everything. Is this something DOM related? How can I send react hook form data to server action?

warm yarrowBOT
#

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

cerulean flume
#

I don't think the useActionState setup would work with RHF. Assuming you are passing the RHF's handleSubmit to the onSubmit of your form, RHF will prevent the default behavior of the form making it not submit the data into the action.

Instead, you will have to call the action function manually inside the handleSubmit of RHF.

Something like this:

<form
  onSubmit={form.handleSubmit(async (data) => {
    const response = await yourAction(data)
  })
>

I would be glad if somehow my understanding is wrong and there is a workaround for this as I had to look into other form libraries recently because of this.

limpid field
#

Hi, hank you for the response. I switched from form action={action} to "onSubmit" and edited the server action, now it's working. Thanks!