I took the example from the documentation :
JavaScript
x
17
17
1
import React from "react";
2
import { useForm } from "react-hook-form";
3
4
export default function App() {
5
const { register, handleSubmit, watch, formState: { errors } } = useForm();
6
const onSubmit = data => console.log(data);
7
8
console.log(watch("example"));
9
10
return (
11
<form onSubmit={handleSubmit(onSubmit)}>
12
<input defaultValue="test" {register("example")} />
13
<input type="submit" />
14
</form>
15
);
16
}
17
But on every change or on submit, I got undefined
for each field
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 ?
Advertisement
Answer
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:
JavaScript
1
14
14
1
function App() {
2
const { register, handleSubmit, watch, formState: { errors } } = useForm();
3
const onSubmit = data => console.log(data);
4
5
console.log(watch("example"));
6
7
return (
8
<form onSubmit={handleSubmit(onSubmit)}>
9
<input defaultValue="test" name="example" ref={register} />
10
<input type="submit" />
11
</form>
12
);
13
}
14