You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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).
Hi and sorry for the delay in fixing this issue. The problem was caused by the fact that the result was not properly reset when the action entered the catch block. In other words, when an internal framework error occurred, the result was not cleared. Now, by resetting the result to {} before re-throwing the error, the onSuccess callback function is correctly executed with data set to undefined, just like when a redirect happens immediately. So it should be fixed in v7.10.3, please let me know if that's the case, and thank you for your patience.
Are you using the latest version of this library?
Is there an existing issue for this?
Describe the bug
When the action redirects,
onError
callback fromuseAction
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:
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:
next-safe-action/packages/next-safe-action/src/hooks.ts
Lines 54 to 58 in dc120df
When
catch((e) => ...
happens because ofNEXT_REDIRECT
, theresult
is not reset, so it stays in"hasErrored"
state, and then the effect inuseActionCallbacks()
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
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
The text was updated successfully, but these errors were encountered: