#How to get previous state from Astro Action?

5 messages · Page 1 of 1 (latest)

vocal zephyr
#

I'm using Astro actions to handle form submissions. The form is built with React (tsx) using useActionState & withState . If I disable JavaScript from the browser and send the form with invalid data, the schema validates the as it should and fails with an error. I can even get the field specific error messages, but I can't get the previous state (the form field values).

This creates an UX problem, since the end-user has to fill-in the form again.

Are there any way to solve this? I.e. return the previous state from the action handler back to the React component?

timber fiber
#

There is the getActionState

vocal zephyr
#

Thanks for the reply! Wasn't counting on anyone to actually reply to this 😅

Ofc entirely possible that I've misunderstood something, but my current understanding and based on some testing/piloting, it seems to me that yes you can get the current state in the handler using getActionState call, but there's no way to return that state back to the React component as "previous state", because if the schema validation fails, the handler code is not called; Presumably because the schema parsing throws and then the Zod Error (or some other error that is derived from it) is returned to the React component (and the Zod Error does not contain the previous state, only information about the validation errors).

#

I need to still investigate more (and maybe wait if there's some other replies that point me to right direction). But might that I have to create a smallest posible demo and create feature request, as I feel this just might be a use case that is not currently possible. Though I believe it is a reasonable use case to be supported somehow.