Server Actions
Leverage oRPC for type-safe and powerful server actions
Introduction
oRPC makes it simple to implement server actions, offering a robust and type-safe way to manage server-side logic. Server actions are supported out of the box and are powered by several key features:
Requirements
To use a procedure as a server action, the procedure must either:
- Be directly callable, or
- Use Calling Procedures with Context to create a callable procedure with context.
Usage
Direct Client Calls
Call server actions directly from client components for greater flexibility:
With Hooks
To handle errors gracefully or use hooks for improved reusability, you can leverage createSafeAction
and useSafeAction
:
Enhanced Form Integration
React forms integrate seamlessly with server actions using createFormAction
. This setup enables automatic type coercion
Thanks to Smart Conversion, and Bracket Notation,
automatically convert 1992
into a bigint
and seamlessly parse objects like user
.
Server Actions with Context
Some procedures cannot be used as server actions directly. This is typically because they
require additional context, such as user information or other runtime data.
In such cases, you can use createProcedureCaller
or createSafeAction
and createFormAction
(built on top of createProcedureCaller
)
to provide the required context dynamically, making the procedure callable and usable as a server action.
This flexibility ensures you can adapt server actions to scenarios requiring runtime information, enhancing usability across diverse use cases.