Skip to content

Commit ba9fd4c

Browse files
committedMar 10, 2024·
docs(website): add React Hook Form integration guide
1 parent 23e05aa commit ba9fd4c

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
sidebar_position: 3
3+
description: Integrate next-safe-action with React Hook Form
4+
---
5+
6+
# React Hook Form integration
7+
8+
next-safe-action works greatly in combo with [React Hook Form](https://react-hook-form.com/).
9+
10+
Here's a guide on how to work with the two libraries together.
11+
12+
:::info
13+
These code snippets are from the [React Hook Form example](https://next-safe-action.vercel.app/react-hook-form) of the example application.
14+
:::
15+
16+
## 1. Define a validation schema
17+
18+
First of all, we need to define the validation schema. We'll use Zod in this case 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:
19+
20+
```typescript title="validation.ts"
21+
import { z } from "zod";
22+
23+
export const schema = z.object({
24+
productId: z.string(),
25+
});
26+
```
27+
28+
## 2. Define the action
29+
30+
Here we define our action. The only difference is that we will import the schema from the file we just created:
31+
32+
```typescript title="buyproduct-action.ts"
33+
"use server";
34+
35+
import { action } from "@/lib/safe-action";
36+
import { schema } from "./validation";
37+
38+
export const buyProduct = action(schema, async ({ productId }) => {
39+
// We're just returning the productId passed to the action here.
40+
return {
41+
productId,
42+
};
43+
});
44+
```
45+
46+
## 3. Define the form
47+
48+
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 adapter 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 inside the form submit handler.
49+
50+
```typescript title="buyproduct-form.tsx"
51+
"use client";
52+
53+
import { zodResolver } from "@hookform/resolvers/zod";
54+
import { useState } from "react";
55+
import { useForm } from "react-hook-form";
56+
import { z } from "zod";
57+
import { buyProduct } from "./buyproduct-action";
58+
import { schema } from "./validation";
59+
60+
export default function BuyProductForm() {
61+
const { register, handleSubmit } = useForm<z.infer<typeof schema>>({
62+
resolver: zodResolver(schema),
63+
});
64+
65+
return (
66+
<form
67+
onSubmit={handleSubmit(async (data) => {
68+
const res = await buyProduct(data);
69+
// Do something useful with the result.
70+
})}>
71+
<input {...register("productId")} placeholder="Product ID" />
72+
<button type="submit">Buy product</button>
73+
</form>
74+
);
75+
}
76+
```

0 commit comments

Comments
 (0)
Please sign in to comment.