TheEdoRan / next-safe-action

Type safe and validated Server Actions in your Next.js project.
https://next-safe-action.dev
MIT License
2.29k stars 35 forks source link

[BUG] `useAction` data can be undefined even `onSuccess` #238

Closed DasOhmoff closed 2 months ago

DasOhmoff commented 2 months ago

Are you using the latest version of this library?

Is there an existing issue for this?

Describe the bug

Hello, thank you for this great plugin!

Since I updated from "next-safe-action": "^7.0.0-next.32" to next-safe-action": "^7.7.0" I get the following error everywhere in my code base.

Example:

export const testAction = actionClient
  .action(async (args) => {
    return "Sample String";
  })

// In component:
const t = useAction(testAction, {
  onSuccess: (args) => {
    arr.push(args.data); // Argument of type string | undefined is not assignable to parameter of type string
  },
});

args.data has the following type: string | undefined, even though if the action succeeds (onSuccess), the returned data is always string. This happens to all types such as objects that get returned from the database.

Maybe I am missing something?

Thanks for your help :)

Reproduction steps

Put code as stated above and see typescript error.

Expected behavior

onSuccess args.data type should not be possibly undefined.

Minimal reproduction example

https://rb.gy/91uk80

Operating System

Windows 10

Library version

7.7.0

Next.js version

14.2.3

Additional context

No response

TheEdoRan commented 2 months ago

Not a bug, this is expected and explained here in detail:

result is undefined on redirect, and onSuccess callback gets called after navigation finishes, so data is undefined in that case. Hope this helps.