Skip to content

Commit

Permalink
chore(website): add link to react-hook-form adapter in integration page
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Aug 18, 2024
1 parent d9d326e commit 214e648
Showing 1 changed file with 4 additions and 66 deletions.
70 changes: 4 additions & 66 deletions website/docs/integrations/react-hook-form.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,72 +7,10 @@ description: Learn how to integrate next-safe-action with React Hook Form.

next-safe-action works great in combo with [React Hook Form](https://react-hook-form.com/).

Here's a guide on how to integrate the two libraries.
By using the react-hook-form adapter, you'll get first-class integration and complete customization for advanced use cases. First, install the following dependencies:

:::info
These code snippets are from the [React Hook Form example](https://next-safe-action-playground.vercel.app/react-hook-form) of the playground app.
:::

## 1. Define a shared validation schema

First of all, we need to define the validation schema. We'll use Zod in this example as our validation library. Note that the schema, in this case, is defined in a separate file, and not in the same file as our action. We have to do this because we need to import the same schema from the actions file, which never leaves the server context, and the Client Component where we use `useForm` from React Hook Form:

```typescript title="validation.ts"
import { z } from "zod";

export const schema = z.object({
productId: z.string(),
});
```

## 2. Define the action

Here we define our action. The only difference is that we will import the schema from the file we just created:

```typescript title="buyproduct-action.ts"
"use server";

import { actionClient } from "@/lib/safe-action";
import { schema } from "./validation";

export const buyProduct = actionClient
.schema(schema)
.action(async ({ parsedInput: { productId } }) => {
// We're just returning the productId passed to the action here.
return {
productId,
};
});
```bash npm2yarn
npm i next-safe-action react-hook-form @hookform/resolvers @next-safe-action/adapter-react-hook-form
```

## 3. Define the form

Lastly, we have to define the form. We'll use `useForm` from React Hook Form to handle the form submission and client side validation. This is a basic example, but can be easily adapted to your needs. Note that we're directly importing the action we just created, instead of using the `useAction` hook from next-safe-action. This is because we're awaiting the action execution inside the form submit handler.

```typescript title="buyproduct-form.tsx"
"use client";

import { zodResolver } from "@hookform/resolvers/zod";
import { useState } from "react";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { buyProduct } from "./buyproduct-action";
import { schema } from "./validation";

export default function BuyProductForm() {
const { register, handleSubmit } = useForm<z.infer<typeof schema>>({
resolver: zodResolver(schema),
});

return (
<form
onSubmit={handleSubmit(async (data) => {
const res = await buyProduct(data);
// Do something useful with the result.
})}>
<input {...register("productId")} placeholder="Product ID" />
<button type="submit">Buy product</button>
</form>
);
}
```
Then, take a look at the project page on GitHub to learn how to use it: [@next-safe-action/adapter-react-hook-form](https://github.com/next-safe-action/adapter-react-hook-form).

0 comments on commit 214e648

Please sign in to comment.