Forms and Controlled Inputs

Keep an input's text in state, enable the button only for a valid email, and handle the submit.

React · beginner · 8 min · 23 steps · free

What you'll learn

ControlledInputs.jsx

import { useState } from 'react';

export default function SignupForm() {
  const [email, setEmail] = useState('');
  const [sent, setSent] = useState(false);

  function handleSubmit(e) {
    e.preventDefault();
    setSent(true);
  }

  if (sent) {
    return <p>Thanks! We will write to {email}.</p>;
  }

  return (
    <form onSubmit={handleSubmit}>
      <input value={email} onChange={(e) => setEmail(e.target.value)} placeholder="Email" />
      <button disabled={!email.includes('@')}>Sign up</button>
    </form>
  );
}

Step by step

  1. React renders <SignupForm> for the first time: it calls the SignupForm function.
  2. useState runs for the first time, so React stores the starting value for email and returns it: "".
  3. useState runs for the first time, so React stores the starting value for sent and returns it: false.
  4. sent is false, so the if-block is skipped.
  5. The input is controlled: its value comes from email, and every change goes through onChange.
  6. The Sign up button is disabled: "" does not include "@".
  7. You type “ada@example.com” into the input. React calls its onChange handler.
  8. onChange gets the event; e.target.value is what is now in the box: "ada@example.com".
  9. React renders <SignupForm> again (render 2): it calls SignupForm once more, from the top.
  10. useState returns the value React has kept for email: "ada@example.com". The starting value is only used on the first render.
  11. useState returns the value React has kept for sent: false. The starting value is only used on the first render.
  12. sent is false, so the if-block is skipped.
  13. SignupForm returns JSX: a description of what the screen should show, <form>…</form>. React has not changed the page yet.
  14. The input shows the new value, and the button is enabled because the email includes "@".
  15. You submit the form. React calls handleSubmit.
  16. e.preventDefault() stops the browser's default action for a form: reloading the page.
  17. setSent(true) does not change sent right away: React queues the update and will render again. In this call sent is still false.
  18. React renders <SignupForm> again (render 3): it calls SignupForm once more, from the top.
  19. useState returns the value React has kept for email: "ada@example.com". The starting value is only used on the first render.
  20. useState returns the value React has kept for sent: true. The starting value is only used on the first render.
  21. sent is true now, so the component returns the thank-you message early.
  22. SignupForm returns JSX: a description of what the screen should show, <p>Thanks! We will write to ada@example.com.</p>. React has not changed the page yet.
  23. React compares the new JSX with the last one and updates only what changed on the page.

Common mistakes

Best practices

Key takeaways

Check yourself

value={email} without onChange makes the input?

Answer: Read-only. React keeps resetting it to email.

Where does the typed text arrive?

Answer: e.target.value

·

More in State and Events