Having a not mandatory numeric input like the following one:
import { useForm } from 'react-hook-form';
export default function App() {
const { register, handleSubmit, formState: { errors } } = useForm();
const onSubmit = data => console.log(data);
console.log(errors);
return (
<form onSubmit={handleSubmit(onSubmit)}>
<input type="number" placeholder="Age" name="Age" ref={register({maxLength: 80}) />
<input type="submit" />
</form>
);
}
I would expect to get a number or null on submit. However I get an string:
{
"Age": ""
}
Or
{
"Age": "10"
}
I could customize the register function as mentioned here to convert it to a number, however I would like to have this behavior by default. This way I wouldn't have to remember to include the function on each input.
I tried to extend useForm but I didn't succeed.
Is there a way to, by default, getting number or null values from numeric inputs?
yup) an option for you?yupcan also be used to transform values.