Course contentsShow
← React
Lesson 522 of 2,39614. Uncontrolled ComponentsFree lesson

Resetting Uncontrolled Forms After Submit

Clear uncontrolled form inputs after successful submission using form.reset() or ref manipulation.

Resetting Uncontrolled Forms After Submit

You'll learn how to clear uncontrolled form inputs after a user successfully submits the form, giving them a fresh slate for the next entry.

The Problem

With uncontrolled components, input values live in the DOM, not in React state. After processing a form submission, you'll often want to clear all the fields so users can enter new data. But since React doesn't control these values, simply calling setState won't help.

Two Practical Solutions

1. Using form.reset()

The simplest approach is calling the native reset() method on the form element itself:

function UncontrolledForm() {
  const handleSubmit = (event) => {
    event.preventDefault();
    const formData = new FormData(event.target);
    
    // Process your data...
    console.log(Object.fromEntries(formData));
    
    // Clear all inputs
    event.target.reset();
  };

  return (
    <form onSubmit={handleSubmit}>
      <input name="username" defaultValue="" />
      <input name="email" defaultValue="" />
      <button type="submit">Submit</button>
    </form>
  );
}

2. Manipulating Refs Directly

If you're using refs to access specific inputs, you can manually clear them:

function UncontrolledForm() {
  const nameRef = useRef(null);
  const emailRef = useRef(null);

  const handleSubmit = (event) => {
    event.preventDefault();
    
    // Process values...
    console.log(nameRef.current.value, emailRef.current.value);
    
    // Clear inputs
    nameRef.current.value = '';
    emailRef.current.value = '';
  };

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

Which to Use?

form.reset() is cleaner when clearing the entire form. Ref manipulation gives you fine-grained control over individual inputs.

Key Takeaway: Clear uncontrolled forms after submission using event.target.reset() for the whole form or by setting ref.current.value = '' for individual inputs.