Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Hooks Form : undefined values on submit

I took the example from the documentation :

import React from "react";
import { useForm } from "react-hook-form";

export default function App() {
  const { register, handleSubmit, watch, formState: { errors } } = useForm();
  const onSubmit = data => console.log(data);

  console.log(watch("example")); 

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input defaultValue="test" {...register("example")} />
      <input type="submit" />
    </form>
  );
}

But on every change or on submit, I got undefined for each field

enter image description here

I tried to install the library again but nothing change and I got undefined everywhere...seems to be a problem with the register function. Does anybody got the same issue ?

like image 302
jossefaz Avatar asked May 09 '21 09:05

jossefaz


People also ask

Is submit successful React hook form?

A critical part of a form entry process is the submission of the form. This part of the process is always interesting when using a 3rd party library such as React Hook Form. One common fundamental task is letting the user know that the submission was successful.

How do I set default values in React hook form?

The solution is to use the reset() function from the React Hook Form library, if you execute the function without any parameters ( reset() ) the form is reset to its default values, if you pass an object to the function it will set the form with the values from the object (e.g. reset({ firstName: 'Bob' }) ).


Video Answer


3 Answers

With v7 the usage of register changed as noted in the comments. If you still need to use v6, you have to write it like this:

function App() {
  const { register, handleSubmit, watch, formState: { errors } } = useForm();
  const onSubmit = data => console.log(data);

  console.log(watch("example")); 

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input defaultValue="test" name="example" ref={register} />
      <input type="submit" />
    </form>
  );
}

Docs v6

Edit React Hook Form - register v6

like image 173
knoefel Avatar answered Oct 21 '22 09:10

knoefel


In my case it was a typo:

<input defaultValue="test" {...(register('name'), { required: true })} />

// submit => { name: undefined }

Instead of:

<input defaultValue="test" {...(register('name', { required: true }))} />

// submit => { name: "test" }

Hopefully it can help someone else.

like image 39
GG. Avatar answered Oct 21 '22 11:10

GG.


In my case, I was using a Controller, so to fix the Undefined value I just had to pass defaultValues to useForm. See the rules section here: https://react-hook-form.com/api/useform/watch

 const { register, handleSubmit, control, setValue} = useForm<MyFormValues>({
    defaultValues : {
      receiveUpdates: false
    }
  });

<Controller
  control={control}
  name="receiveUpdates"
  render={({ field }) => (
    <FormControlLabel
      control={
        <Checkbox
          ref={field.ref}
          checked={field.value}
          onChange={field.onChange}
        />
      }
      label="Receive Email Updates?"
      labelPlacement="start"
    />
  )}
/>
like image 1
Philip C. Avatar answered Oct 21 '22 11:10

Philip C.