DEV Community

Coder
Coder

Posted on

Clearing the input field value in React

If you've ever created a form in React, you've probably run into the issue of clearing the input field value once the form is submitted. It's a common problem that can be frustrating to solve, but fear not! This guide will walk you through the process of clearing the input field value in React, step-by-step.

Step 1: Create a Controlled Component

The first step in clearing the input field value in React is to create a controlled component. A controlled component is a form element whose value is controlled by React.

To create a controlled component, you'll need to add state to your component. In this example, we'll be creating a simple form with a single input field:

import React, { useState } from "react";

function MyForm() {
  const [value, setValue] = useState("");

  function handleSubmit(event) {
    event.preventDefault();
    console.log("Submitted:", value);
  }

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

export default MyForm;
Enter fullscreen mode Exit fullscreen mode

In this example, we're creating a useState hook to add state to our component. We're also creating a handleSubmit function that will log the submitted value to the console.

The return statement contains our form element, with an onSubmit event attached to the handleSubmit function. Inside the form, we have a label and input field. Notice that the value of the input field is set to value, which is the state we created earlier. We're also adding an onChange event to update the value state whenever the input field changes.

Step 2: Add a Ref to the Input Field

The next step in clearing the input field value in React is to add a ref to the input field. A ref is a way to access the DOM node for a component. We'll use this ref to clear the value of the input field.

To add a ref to our input field, we'll modify our component like this:

import React, { useState, useRef } from "react";

function MyForm() {
  const [value, setValue] = useState("");
  const inputRef = useRef(null);

  function handleSubmit(event) {
    event.preventDefault();
    console.log("Submitted:", value);
    inputRef.current.value = "";
  }

  return (
    <form onSubmit={handleSubmit}>
      <label>
        Name:
        <input
          type="text"
          value={value}
          onChange={e => setValue(e.target.value)}
          ref={inputRef}
        />
      </label>
      <button type="submit">Submit</button>
    </form>
  );
}

export default MyForm;
Enter fullscreen mode Exit fullscreen mode

In this example, we're adding a useRef hook to create a ref for our input field. We're also modifying our handleSubmit function to clear the value of the input field by setting inputRef.current.value to an empty string.

Finally, we're adding the ref attribute to our input field and setting it to inputRef.

Step 3: Test the Clear Functionality

Once you've added the ref to your input field and updated your handleSubmit function, you're ready to test the clear functionality.

When you submit the form, the console should log the submitted value and the input field should be cleared. If you try submitting the form again without entering any new values, you should see that the input field remains cleared.

Congratulations! You've successfully cleared the input field value in React.

Conclusion

Clearing the input field value in React may seem like a small task, but it's an important one for creating a good user experience. By creating a controlled component and adding a ref to the input field, you can easily clear the value and make your forms more user-friendly.

As always, make sure to thoroughly test your code and consider edge cases to ensure that your form behaves as expected. Happy coding!

Top comments (0)