TheEdoRan / next-safe-action

Type safe and validated Server Actions in your Next.js (App Router) project.
https://next.next-safe-action.dev
BSD 3-Clause "New" or "Revised" License
1.38k stars 26 forks source link

[FEATURE] [v7] Support stateful actions using React `useActionState` hook #91

Closed TheEdoRan closed 3 weeks ago

TheEdoRan commented 2 months ago

useActionState will replace the previous useFormState hook. When the new hook will be available in Next.js, we can start working on the Form Actions support.

And since that useActionState will work both with and without form elements, it means that probably there's no need to introduce a new hook in next-safe-action. My current idea is to update useAction and useOptimisticAction hooks to use useActionState under the hood. Action signature will change if this will be the case, since the action requires previousState to be the argument placed between optional bind params and form data, in the function called by the client.

UPDATE: implemented stateful action support in the library via stateAction instance method and useStateAction hook. Updated useOptimisticAction API too.

When this PR gets merged, updated documentation will be available here:


Please note that React's useActionState hook is available in Next.js since 14.3.0-canary version, so once the PR gets merged you are required to use a canary release. When the support for it will land in a Next.js stable version, the next branch of next-safe-action will be merged into main, and version 7 will be released with all the new features.

TheEdoRan commented 1 month ago