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
feat(hooks): return executeAsync from useAction and useOptimisticAction hooks (#147)
This PR adds the `executeAsync` function to the return object of `useAction` and `useOptimisticAction` hooks. It's currently not possible to add it to the `useStateAction` hook. As the name suggests, `executeAsync` returns a Promise with the same result of the `safeActionFn` you pass to the hook, and it allows to await the result of the action.
re #137, #72, #94
|`execute`|`(input: InferIn<S>) => void`| An action caller with no return. The input is the same as the safe action you passed to the hook. |
78
+
|`executeAsync`|`(input: InferIn<S>) => Promise<Awaited<ReturnType<typeof safeActionFn>>>`| An action caller that returns a promise with the return value of the safe action. The input is the same as the safe action you passed to the hook. |
78
79
|`input`|`InferIn<S> \| undefined`| The input passed to the `execute` function. |
79
80
|`result`|[`HookResult`](/docs/types#hookresult)| When the action gets called via `execute`, this is the result object. |
80
81
|`reset`|`() => void`| Programmatically reset `input` and `result` object with this function. |
|`execute`|`(input: InferIn<S>) => void`| An action caller with no return. The input is the same as the safe action you passed to the hook. |
138
+
|`executeAsync`|`(input: InferIn<S>) => Promise<Awaited<ReturnType<typeof safeActionFn>>>`| An action caller that returns a promise with the return value of the safe action. The input is the same as the safe action you passed to the hook. |
138
139
|`input`|`InferIn<S> \| undefined`| The input passed to the `execute` function. |
139
140
|`result`|[`HookResult`](/docs/types#hookresult)| When the action gets called via `execute`, this is the result object. |
140
141
|`optimisticState`|`State`| This contains the state that gets updated immediately after `execute` is called, with the behavior you defined in the `updateFn` function. The initial state is what you provided to the hook via `currentState` argument. If an error occurs during action execution, the `optimisticState` reverts to the state that it had pre-last-last action execution. |
Copy file name to clipboardexpand all lines: website/docs/migrations/v6-to-v7.md
+4
Original file line number
Diff line number
Diff line change
@@ -157,6 +157,10 @@ Sometimes it's useful to access the input passed to an action when using hooks.
157
157
158
158
Starting from version 7, `isIdle`, `isExecuting`, `hasSucceeded` and `hasErrored` are returned from hooks, in addition to the `status` property. This is the same behavior of next-safe-action pre-v4 and very similar to the [TanStack Query](https://tanstack.com/query/latest) API.
159
159
160
+
### [Return `executeAsync` from `useAction` and `useOptimisticAction` hooks](https://github.com/TheEdoRan/next-safe-action/issues/146)
161
+
162
+
Sometimes it's useful to await the result of an action execution. Starting from version 7, `executeAsync` is returned from `useAction` and `useOptimisticAction` hooks. It's essentially the same as the original safe action function, with the added benefits from the hooks. It's currently not possible to add this function to the `useStateAction` hook, due to internal React limitations.
0 commit comments