An open API service indexing awesome lists of open source software.

https://github.com/jackh-sh/use-server-action

A type-safe React hook and utilities for working with Next.js server actions.
https://github.com/jackh-sh/use-server-action

nextjs react reactjs server-side server-side-rendering serveractions

Last synced: about 2 months ago
JSON representation

A type-safe React hook and utilities for working with Next.js server actions.

Awesome Lists containing this project

README

          

# use-server-action

A type-safe React hook and utilities for working with Next.js server actions.

## Installation

```bash
npm install use-server-action
```

## Quick Start

### 1. Create a server action

```ts
// app/actions.ts
"use server";

import { serverAction, success, error } from "use-server-action/server";

export const createUser = serverAction(async (name: string) => {
if (!name.trim()) {
throw new Error("Name is required");
}

const user = await db.user.create({ data: { name } });
return user;
});

// Or handle errors manually for more control:
export const deleteUserAction = async (id: string) => {
try {
await db.user.delete({ where: { id } });
return success({ deleted: true });
} catch (e) {
return error("Failed to delete user", "DELETE_FAILED");
}
};
```

### 2. Use in a client component

```tsx
"use client";

import { useServerAction } from "use-server-action";
import { createUser } from "./actions";

export function CreateUserForm() {
const {
execute,
data,
error,
isPending,
isSuccess,
isError,
reset,
} = useServerAction({
action: createUser,
onSuccess: (user) => {
console.log("User created:", user);
},
onError: (message, code) => {
console.error(`Error [${code}]:`, message);
},
});

return (
execute(formData.get("name") as string)}>


{isPending ? "Creating..." : "Create User"}

{isError &&

{error}

}
{isSuccess &&

Created: {data?.name}

}

);
}
```

## Documentation

You can view the documentation [here](https://use-server-action.jackh.sh)

## License

MIT