Next.js Discord

Discord Forum

How to trigger a Server Action from an `onBlur` event?

Unanswered
Cassin's Sparrow posted this in #help-forum
Open in Discord
Cassin's SparrowOP
How to trigger a Server Action from an onBlur event?

12 Replies

Cassin's SparrowOP
I'm assuming this can be done with useTransition, can I still use useOptimistic then?
That can be done by useTransition and useOptimistic also works without a form, but I usually recommend using a route handler instead.
Cassin's SparrowOP
Awesome! Why would you recommend using a route handler instead?
Cassin's SparrowOP
I've tried and it works! Just curious @fuma, why would you recommend a route handler instead?
Because developers usually use fetch with client-side revalidations in production applications. In these cases route handler will be a better choice
Cassin's SparrowOP
useOptimistic doesn't work with route handlers, right?
useOptimistic is just a hook provided by React, it can be used with anything
Cassin's SparrowOP
That makes sense, thanks for your help!
Cassin's SparrowOP
One more question @fuma , can you get the returned value from the Server Action with useTransition?
Although server actions are not used for data fetching, but you can receive the retuned value since startTransition accepts an async function
Cassin's SparrowOP
Thanks! 🔥
btw if your problem has been solved, you can mark my answer as the solution :love: