
Company News
Free Business Plan Upgrades for Open Source Maintainers
Open source maintainers are under more pressure than ever. We're raising our open source program from the Team plan to the Business plan, free.
use-safe-server-action
Advanced tools
A React hook for safely handling server actions with loading, success, and error states.
A React hook for safely handling server actions with loading, success, and error states.
npm install use-safe-server-action
import { useSafeServerAction } from '@your-package/safe-server-action';
// Define your server action
"use server";
async function createUser(userData: UserData) {
// Your server action logic here
return await db.users.create(userData);
}
"use client";
function UserForm() {
const { state, data, errors, mutate } = useSafeServerAction(createUser);
const handleSubmit = (formData: UserData) => {
mutate(formData);
};
return (
<div>
{state.isLoading && <div>Loading...</div>}
{state.isError && <div>Error: {errors?.message}</div>}
{state.isSuccess && <div>User created successfully!</div>}
<form onSubmit={handleSubmit}>
{/* Your form fields */}
</form>
</div>
);
}
function UserForm() {
const { mutateAsync } = useSafeServerAction(createUser);
const handleSubmit = async (formData: UserData) => {
const response = await mutateAsync(formData);
if (response.success) {
// Handle success
console.log(response.data);
} else {
// Handle error
console.error(response.error);
}
};
}
useSafeServerAction<TData, TError>serverFn: (...args: any[]) => Promise<TData> - The server action function to execute{
state: {
isLoading: boolean;
isError: boolean;
isSuccess: boolean;
};
data: TData | null;
errors: TError | null;
mutate: (...args: any[]) => void;
mutateAsync: (...args: any[]) => Promise<ApiResponse<TData, TError>>;
}
isLoading: Indicates if the server action is in progressisError: Indicates if the server action failedisSuccess: Indicates if the server action completed successfullydata: Contains the successful response dataerrors: Contains error information if the action failedmutate: Executes the server action without returning a promisemutateAsync: Executes the server action and returns a promise with the resultThe hook is fully typed and supports generic type parameters:
interface User {
id: string;
name: string;
}
interface ApiError {
code: string;
message: string;
}
const { data, errors } = useSafeServerAction<User, ApiError>(createUser);
// data is typed as User | null
// errors is typed as ApiError | null
MIT
FAQs
A React hook for safely handling server actions with loading, success, and error states.
We found that use-safe-server-action demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Open source maintainers are under more pressure than ever. We're raising our open source program from the Team plan to the Business plan, free.

Security News
The supply chain control that delays freshly published gems now covers lockfile generation and gem vendoring in Ruby projects.

Security News
During a UK cyber test, a Mythos 5 agent used sockpuppets, social engineering, and prompt injection to try to get a maintainer to merge malware.