Return dispatcher from useStateAction #190
-
Hi, thanks for your nice work! The useStateAction hook passed the safeActionFn to react's useActionState, is it feasible to return the dispatcher from useStateAction, so that I can pass it to the form's action prop for non-js situation? some code like: import {.action } from './action'
const App = () => {
const { result, dispatcher, execute } = useStateAction(action, {})
return (
<form
action={dispatcher}
onSubmit={(e) => {
e.preventDefault()
excute(new FormData(e.currentTarget))
}}
>
{result.data?.someValue}
<button>submit</button>
</form>
)
} it seems to work well, did I miss something? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 12 replies
-
Saw you already commented on the related issue, but I'm linking my reply here too just for context. As already said, I'm thinking of changing |
Beta Was this translation helpful? Give feedback.
-
I recently heard about this library and do not understand the principles by which it works and provides this type safety. I thought it was just a convenient wrapper for validation, so as not to constantly talk like this: const action = async (
prevState: unknown,
formData: FormData,
) => {
const field = formData.get('email')?.toString();
const valid = await schema.safeParseAsync(field);
} At the same time, with nice features, for example: validation on the client side The idea is very good and it is great if this library will be finalized. But the library is made very non-native. When everyone is striving for progressive improvement and making it all native (the same resurrection of the action attribute of a form and tailwind, which is trying to more embed native CSS in the new version), the library does not like to emphasize it, provided that it is not a recently added feature. There was a
Why can't you just make a wrapper for useActionState, like Something like this: const someAction = action.schema(schema)
.action(async (prevState, valid) => { ... }) const Component = () => {
const { response, formAction, isPending } = useSafeActionState(someAction, null)
return (
<form
action={formAction}
onSubmit={e => {
e.preventDefault()
formAction(new FormData(e.currentTarget))
}}
>...</form>
)
} I think if we make it more native, then people will use this library more actively. To simply optimize routine actions related to validation |
Beta Was this translation helpful? Give feedback.
-
You can discuss this with the developers of react-hook-form. They are wondering how to make it all type-safe using server actions. Think about the same thing. Maybe you can make a convenient wrapper that can be used by react-hook-form and then the beauty will be |
Beta Was this translation helpful? Give feedback.
Problem is that if users use
dispatcher
instead ofexecute
, then input and status properties go out of sync, which is not a good DX, as already said above. Returningdispatcher
fromuseStateAction
would allow writing buggy code, and I already can see the number of issues that would be opened if I implemented that.So, not sure if there's a good solution. If you want progressive enhancement, it's probably best to use the
useActionState
hook from React. Let me know your thoughts as well, thanks.