I'm trying to understand how react-hook-form is working. For this I created the following example:
import React from 'react';
import { useForm } from 'react-hook-form';
const InputForm = () => {
const { register, handleSubmit } = useForm();
const onSubmit = (data) => {
console.log(data);
};
return (
<>
<Form onSubmit={handleSubmit(onSubmit)}>
<Form.Group controlId='formBasicName'>
<Form.Label>Name</Form.Label>
<Form.Control
type='text'
name='name'
ref={register}
/>
</Form.Group>
<Form.Group controlId='formBasicEmail'>
<Form.Label>Email address</Form.Label>
<Form.Control
type='email'
name='email'
ref={register}
/>
<Button className='submitBtn' type='submit'>
Submit
</Button>
</Form>
</>
);
};
export default InputForm;
It works and if I submit the form I can see the data object in console. The only problem is that after each submit the input boxes are still showing their value. I want that after each submit the value of input boxes get empty. That would be easy with useState, but now that I'm using react-hook-from, I don't know how to do that.
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' }) ).
React Hook Form relies on uncontrolled form, which is the reason why the register function capture ref and controlled component has its re-rendering scope with Controller or useController .
To check if an input is empty in React:Call the trim() method on the field's value. Access the length property on the value. If the field's value has a length of 0 , then it is empty, otherwise it isn't.
Clearing the input field values If you are using controlled components, it means your form (input) data is controlled by the react state, so that you can clear an input field value by assigning an empty string '' to the react state. Here is an example:
Store the values of the input fields in state variables. Set the onSubmit prop on the form element. When the submit button is clicked, set the state variables to empty strings. The button element in the form has type of submit, so every time it is clicked the submit event is fired on the form.
You can easily submit form asynchronously with handleSubmit. disabled inputs will appear as undefined values in form values. If you want to prevent users from updating an input and wish to retain the form value, you can use readOnly or disable the entire <fieldset />. Here is an example.
When the submit button is clicked, set the state variables to empty strings. The button element in the form has type of submit, so every time it is clicked the submit event is fired on the form. We used the event.preventDefault () method in the handleSubmit function to prevent the page from refreshing when the form is submitted.
const InputForm = () => {
const { register, handleSubmit, reset } = useForm();
const onSubmit = (data) => {
//...
reset();
};
Add it to the submit function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With