Skip to content

Instantly share code, notes, and snippets.

@ryands17
Forked from danieljpgo/useForm.ts
Created March 5, 2023 19:03
Show Gist options
  • Save ryands17/ef1ab7307f88402b055f34247e736cd3 to your computer and use it in GitHub Desktop.
Save ryands17/ef1ab7307f88402b055f34247e736cd3 to your computer and use it in GitHub Desktop.
react-hook-form + zod
import {
useForm as useHookForm,
UseFormProps as useHookFormProps,
} from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { TypeOf, ZodSchema } from "zod";
interface UseFormProps<Z extends ZodSchema>
extends Exclude<useHookFormProps<TypeOf<Z>>, "resolver"> {
schema: Z;
}
export function useForm<Z extends ZodSchema>({
schema,
...props
}: UseFormProps<Z>) {
return useHookForm({ ...props, resolver: zodResolver(schema) });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment