Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] onError called on redirects if it was called previously #303

Open
2 tasks done
berekuk opened this issue Dec 1, 2024 · 0 comments
Open
2 tasks done

[BUG] onError called on redirects if it was called previously #303

berekuk opened this issue Dec 1, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@berekuk
Copy link

berekuk commented Dec 1, 2024

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

When the action redirects, onError callback from useAction parameter will be called again if it was called previously.

This can cause incorrect repeat notifications of stale errors.

I have a form component that does something like this:

  const { executeAsync, status } = useAction(myAction, {
    onError: ({ error }) => {
      toast(error);
    },
  });

  // this is react-hook-form, but shouln't matter
  const onSubmit = form.handleSubmit(executeAsync);

  ...

And an action that can fail based on the form data. When the action succeeds, it calls redirect(...).

The problem is: if the action is called once and resulted in an error, and then it's called again with the correct data, then on the second call onError will be called again, and there'll be an extra confusing toast for that older error.

I tried to trace this issue through the codebase, and in my understanding it's caused by this code:

safeActionFn(input as S extends Schema ? InferIn<S> : undefined)
.then((res) => setResult(res ?? {}))
.catch((e) => {
throw e;
})

When catch((e) => ... happens because of NEXT_REDIRECT, the result is not reset, so it stays in "hasErrored" state, and then the effect in useActionCallbacks() fires once again with that stale errored result (for some reason; I'm not sure which effect dep causes the callbacks effect to fire).

Reproduction steps

  1. Start the https://github.com/berekuk/next-safe-action-redirect-error
  2. Press "fail".
  3. Check console; there will be the first "ERROR" in logs.
  4. Press "redirect"; see how there's a second "ERROR" being logged on redirect.

Expected behavior

On the second button click, onError shouldn't be called.

Link to a minimal reproduction of the issue

https://github.com/berekuk/next-safe-action-redirect-error

Operating System

macOS

Library version

7.9.9

Next.js version

15.0.3

Node.js version

v20.15.1

Additional context

No response

@berekuk berekuk added the bug Something isn't working label Dec 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant