Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-hook-form - empty input field after each submit

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.

like image 672
user1941537 Avatar asked Jul 05 '20 13:07

user1941537


People also ask

How do you clear input field after submit in React hooks?

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' }) ).

Is React hook form uncontrolled?

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 .

How do you check if all inputs are not empty React?

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.

How do I clear the input field values in react?

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:

How do I make a form submit every time it clicks?

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.

How to submit Form asynchronously with handlesubmit?

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.

How to prevent page from refreshing when Submit button is clicked?

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.


Video Answer


1 Answers

const InputForm = () => {
  const { register, handleSubmit, reset } = useForm();

  const onSubmit = (data) => {
    //...
    reset();
  };

Add it to the submit function

like image 196
Olcay Özdemir Avatar answered Oct 09 '22 20:10

Olcay Özdemir