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

[FEATURE] Return async function from hooks #72

Closed
2 tasks done
pharmarin opened this issue Feb 29, 2024 · 6 comments
Closed
2 tasks done

[FEATURE] Return async function from hooks #72

pharmarin opened this issue Feb 29, 2024 · 6 comments
Labels
enhancement New feature or request

Comments

@pharmarin
Copy link

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

Suggest an idea

I would like to have an async function returned from hooks. This function would be executed from event handlers to set internal state with more granularity.

Additional context

No response

@pharmarin pharmarin added the enhancement New feature or request label Feb 29, 2024
@TheEdoRan
Copy link
Owner

TheEdoRan commented Mar 1, 2024

Hi @pharmarin, I don't think this is possible, since next-safe-action hooks use the useTransition hook from React under the hood. If you need to call Server Actions from event handlers, I suggest to directly import them inside your Client Components.

Please let me know if this solves your issue, thanks.

@pharmarin
Copy link
Author

I really need to await and catch the call while getting current status in my component. So I "created" a hook that does the work

@TheEdoRan
Copy link
Owner

Unfortunately this isn't currently supported, and it isn't a next-safe-action issue, so there's not much I can do to fix it. Please track this issue from React to get updates regarding this. At this time, though, React documentation states:

The function you pass to startTransition must be synchronous. React immediately executes this function, marking all state updates that happen while it executes as transitions. If you try to perform more state updates later (for example, in a timeout), they won’t be marked as transitions.

React recommends to use useTransition hook to handle mutations on the client, but if you don't need to use it in your specific case, creating a custom hook as you did is acceptable, I think. Hope this helps.

@pharmarin
Copy link
Author

This will be perfect when it gets merged : facebook/react#28491

@TheEdoRan
Copy link
Owner

TheEdoRan commented Mar 7, 2024

Yes @pharmarin! That hook will surely change the way we handle Server Actions in next-safe-action, and also pave the way for Form Actions support.

@TheEdoRan TheEdoRan mentioned this issue Apr 4, 2024
2 tasks
@TheEdoRan
Copy link
Owner

TheEdoRan commented Apr 29, 2024

I'm implementing stateful actions in the library right now, since the new hook is finally available in Next.js, and I just found out that useActionState doesn't return an async dispatcher, it returns a function with this signature instead:

dispatcher: () => void

So apparently it's not going to solve this issue.

Repo with a test: next-useactionstate-test.

re #94

TheEdoRan added a commit that referenced this issue May 27, 2024
…cAction` 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants