How to Clear Input Field After Submit in React

When developing web applications with React, it’s common to encounter scenarios where you need to clear input fields after a form submission. This functionality is essential for providing a smooth user experience and ensuring that input fields are ready for new data entry. In this article, we will explore different approaches to achieve this in React.

Method 1: Using State to Clear Input Fields

One way to clear input fields after a form submission is by utilizing React’s state management. Here’s an example of how you can implement this approach:

  1. Start by creating a class component or a functional component with hooks.
  2. Initialize the state variable that will hold the input field values.
  3. Bind the input fields to their respective state variables using the value prop and onChange event handler.
  4. When the form is submitted, handle the submission event by updating the state variables to empty values.
  5. React’s re-rendering mechanism will automatically update the input fields with the new values.

Here’s a code snippet demonstrating this method:

import React, { useState } from 'react';

function ClearInputField() {
  const [inputValue, setInputValue] = useState('');

  const handleSubmit = (e) => {
    e.preventDefault();
    // Perform your form submission logic here

    // Clear input field after submit
    setInputValue('');
  };

  return (
    <form onSubmit={handleSubmit}>
      <input
        type="text"
        value={inputValue}
        onChange={(e) => setInputValue(e.target.value)}
      />
      <button type="submit">Submit</button>
    </form>
  );
}

export default ClearInputField;

By updating the inputValue state variable to an empty string (''), the input field will be cleared after each form submission.

Method 2: Using Refs to Clear Input Fields

Another approach to clearing input fields after submit in React is by using refs. This method is especially useful when working with uncontrolled components or when you need to clear multiple input fields simultaneously. Here’s how you can implement it:

  1. Create a ref using the useRef hook.
  2. Attach the ref to the input field using the ref attribute.
  3. When the form is submitted, access the input field’s value through the ref and update it to an empty string.

Here’s an example code snippet demonstrating this method:

import React, { useRef } from 'react';

function ClearInputField() {
  const inputRef = useRef(null);

  const handleSubmit = (e) => {
    e.preventDefault();
    // Perform your form submission logic here

    // Clear input field after submit
    inputRef.current.value = '';
  };

  return (
    <form onSubmit={handleSubmit}>
      <input type="text" ref={inputRef} />
      <button type="submit">Submit</button>
    </form>
  );
}

export default ClearInputField;

By setting the value of the input field’s ref (inputRef.current.value) to an empty string, the input field will be cleared after each form submission.

author's bio photo

Hi there! I am Avic Ndugu.

I have published 100+ blog posts on HTML, CSS, Javascript, React and other related topics. When I am not writing, I enjoy reading, hiking and listening to podcasts.