Handlesubmit function of hookform

Dear All

I am learning hook form
when I use handlesubmit function of it and a function as code line 12, everything is fine
but when I want to create this logic as a function which is in line 6 , I face the error that the type of data is not define .however when I check the line 12, the type of data is the same as what I type in line 6.
Does anybody know the reason?

Hello Razieh,

You need to specify a type for your form and associate it with your data object.
See FormValues below.

import {useForm } from 'react-hook-form'

type FormValues = {
    name: string
    age: number

const Form = () => {
  const {handleSubmit, register} = useForm<FormValues>()
  const onSubmit = (data: FormValues) => console.log(data)

    return (
        <form onSubmit={handleSubmit(onSubmit)}>
        <input {...register("name")} />
        <input {...register("age")} />
        <input type="submit" />

You can learn more about the handleSubmit function here:

import {FieldValues} from “react-hook-form”

hope this should fix your error

1 Like

That was a bit easier :slight_smile:

1 Like