Skip to content

Commit 42e348c

Browse files
committed
v7 with middleware
1 parent c05d320 commit 42e348c

File tree

9 files changed

+3267
-2733
lines changed

9 files changed

+3267
-2733
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,41 @@ you need to know about form handling in React Router/Remix. It also covers how t
232232
https://youtu.be/iom5nnj29sY?si=l52WRE2bqpkS2QUh
233233

234234

235+
## Middleware mode
236+
237+
From v7 you can use middleware to extract the form data and access it anywhere in your actions and loaders.
238+
All you have to do is set it up in your `root.tsx` file like this:
239+
240+
```ts
241+
import { unstable_extractFormDataMiddleware } from "remix-hook-form/middleware";
242+
243+
export const unstable_middleware = [unstable_extractFormDataMiddleware()];
244+
```
245+
246+
247+
And then access it in your actions and loaders like this:
248+
249+
250+
```ts
251+
import { getFormData, getValidatedFormData } from "remix-hook-form/middleware";
252+
253+
export const loader = async ({ context }: LoaderFunctionArgs) => {
254+
const searchParamsFormData = await getFormData(context);
255+
return { result: "success" };
256+
};
257+
export const action = async ({ context }: ActionFunctionArgs) => {
258+
// OR: const formData = await getFormData(context);
259+
const { data, errors, receivedValues } = await getValidatedFormData<FormData>(
260+
context,
261+
resolver,
262+
);
263+
if (errors) {
264+
return { errors, receivedValues };
265+
}
266+
return { result: "success" };
267+
};
268+
```
269+
235270
## API's
236271

237272
### getValidatedFormData
@@ -347,6 +382,7 @@ export const action = async ({ request }: Route.ActionArgs) => {
347382

348383
```
349384

385+
350386
### getFormDataFromSearchParams
351387

352388
If you're using a GET request formData is not available on the request so you can use this method to extract your formData from the search parameters assuming you set all your data in the search parameters

0 commit comments

Comments
 (0)