Running a SafeFn
useServerAction hook
The useServerAction()
hook makes calling a SafeFn from the client easy.
It handles transitioning, exposes loading states and converts the Promise<ActionResult>
object back into a ResultAsync
.
Arguments
SafeFn Action
The action
argument is the resulting function from calling createAction()
on a SafeFn.
Callbacks
The useServerAction()
hook optionally takes in the following callbacks functions that will run on the client, and the properties of the argument object they can take:
onStart()
- Called whenexecute()
is called.unsafeRawInput
: The raw input passed intoexecute()
.
onSuccess()
- Called when theexecute()
returns anOk
.unsafeRawInput
: The raw input passed intoexecute()
.value
: The value returned from the action. See here for the type.
onError()
- Called when theexecute()
returns anErr
.unsafeRawInput
: The raw input passed intoexecute()
.error
: the error returned from the action. See here for the type.
onComplete()
- Called when theexecute()
returns.unsafeRawInput
: The raw input passed intoexecute()
.result
: The result returned from the action. See here for the type.
These callbacks are not awaited. This means onStart()
does not block the
action from running, and the other callbacks do not block hook from returning
the result from the action.
Returns
An object with the following properties:
execute()
: A function that takes in the argument to run the action, the same as the argument forrun()
.result
: theResult
of runningexecute()
. See the dedicated page for the type of this.isPending
: a boolean representing if the action is currently executing or transitioning.isSuccess
: a boolean representing if the action completed successfully.