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

Common Pattern: Reading on Submit

Handle form submission by reading all input values from refs in the submit handler, avoiding intermediate renders.

Common Pattern: Reading on Submit

What you'll learn: How to read all input values from refs when a form is submitted, keeping your component efficient by avoiding re-renders on every keystroke.

The Pattern

The most common and practical use of uncontrolled components is reading values only when you need them—typically when a form is submitted. Instead of syncing state on every change, you attach refs to each input and read ref.current.value inside your submit handler.

How It Works

function LoginForm() {
  const emailRef = useRef(null);
  const passwordRef = useRef(null);

  function handleSubmit(event) {
    event.preventDefault();
    
    const email = emailRef.current.value;
    const password = passwordRef.current.value;
    
    // Now do something with these values
    console.log({ email, password });
  }

  return (
    <form onSubmit={handleSubmit}>
      <input type="email" ref={emailRef} />
      <input type="password" ref={passwordRef} />
      <button type="submit">Log In</button>
    </form>
  );
}

Why This Pattern Works Well

You don't need to know what the user typed until they hit submit. By reading refs at that moment, you avoid:

  • Re-rendering on every keystroke
  • Managing multiple pieces of state
  • onChange handlers for each input

The component renders once, the user types freely (no React involved), and when they submit, you grab all the values in one go.

When to Use This

This pattern shines for simple forms where you don't need live validation, formatting, or conditional rendering based on input values. If your form just collects data and sends it somewhere, uncontrolled components with submit-time reading are simpler and faster.

Key Takeaway: Reading refs in the submit handler lets you collect all form data efficiently without intermediate re-renders—perfect for straightforward forms.