next-safe-action: [BUG] Sending form with javascript disabled not working

Are you using the latest version of this library?

  • I verified that the issue exists in the latest next-safe-action release

Is there an existing issue for this?

  • I have searched the existing issues and found nothing that matches

Describe the bug

Sending form with js disabled does not work when using useAction hook.

Reproduction steps

  1. Go to playground https://next-safe-action-playground.vercel.app/stateless-form
  2. Disable javascript in devtools
  3. Try sending the form

See attached screenshot of the form element CleanShot 2024-07-02 at 12 48 01@2x

Expected behavior

Form should be sent with javascript disabled, at least that’s what I understand from looking at the example in the docs https://next-safe-action.dev/docs/recipes/form-actions

Minimal reproduction example

https://next-safe-action-playground.vercel.app/stateless-form

Operating System

macOS

Library version

7.1.3

Next.js version

15.0.0-rc.0

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 17 (6 by maintainers)

Most upvoted comments

Added a section about progressive enhancement on the website: https://next-safe-action.dev/docs/execution/hooks/usestateaction#progressive-enhancement

@tenghuan123 thank you for your feedback. This issue has also been discussed here:

It would be great to return the dispatcher from useActionState hook too, and it would also be really simple to implement. The problem, as explained in the discussion, is this:

Problem is that if users use dispatcher instead of execute, then input and status properties go out of sync […] Returning dispatcher from useStateAction 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.

Hey, BTW, do you guys can use the bindArgsSchemas ? I’m new on this so that’s the reason I didn’t open a new issue, and it could be an error on my side. This is the error that I get

The 'this' context of type 'SafeActionFn<string, ZodObject<{ username: ZodString; }, "strip", ZodTypeAny, { username: string; }, { username: string; }>, [userId: ZodString, age: ZodNumber], any, any, { ...; }>' is not assignable to method's 'this' of type '(this: null, args_0: string, args_1: number, ...args_2: any[]) => Promise<SafeActionResult<string, ZodObject<{ username: ZodString; }, "strip", ZodTypeAny, { username: string; }, { username: string; }>, ... 4 more ..., unknown> | undefined>'.
  Types of parameters 'bindArgsInputs' and 'args_0' are incompatible.
    Type '[string, number, ...any[]]' is not assignable to type '[...bindArgsInputs: any[], input: any]'.
      Source provides no match for required element at position 1 in target.ts(2684)
(alias) const onboardUser: SafeActionFn<string, z.ZodObject<{
    username: z.ZodString;
}, "strip", z.ZodTypeAny, {
    username: string;
}, {
    username: string;
}>, [userId: z.ZodString, age: z.ZodNumber], any, any, {
    ...;
}>
import onboardUser

As @tenghuan123 said, progressive enhancement is available via useActionState hook from React.

next-safe-action exports a hook called useStateAction from /stateful-hooks path, but I’ve just tested it and it doesn’t work with JavaScript disabled. The execute function returned from that hook is a wrapper of the dispatcher from React’s useActionState hook. This is because otherwise useful props (such as status and client input) couldn’t be returned from the hook — see the image below. *

Now I’m actually thinking of changing the useStateAction API to just return the [state, action, isPending] original array or remove the stateful hook entirely from the library and tell users to use the React one. If this will be the case, the useStateAction hook will be marked as deprecated for a number of minor versions and then removed.

What do you think?

* useStateAction return object: useStateAction return object

Thanks for making it clear. I think it could be a good idea to clearly state it in the docs that if you want progressive enhancement you need to use react native hook.