useAction
Edit this pageuseAction returns a function that calls an action with the current router context. It is the programmatic caller for non-form submissions.
Import
import { useAction } from "@solidjs/router";Type
function useAction<T extends Array<any>, U, V>( action: Action<T, U, V>): (...args: Parameters<Action<T, U, V>>) => Promise<NarrowResponse<U>>;Parameters
action
- Type:
Action<T, U, V> - Required: Yes
Action to bind to the current router.
Return value
- Type:
(...args: Parameters<Action<T, U, V>>) => Promise<NarrowResponse<U>>
Returns a router-bound caller with the same arguments as action.
Behavior
- Unlike native form submissions, calls made with
useActiondepend on client-side JavaScript.
Examples
Basic usage
import { action, useAction } from "@solidjs/router";
const likePost = action(async (id: string) => { return id;}, "likePost");
function LikeButton(props: { id: string }) { const like = useAction(likePost);
return <button onClick={() => like(props.id)}>Like</button>;}