liveBook · Manning

Form submission

As explained in React Quickly, Second Edition

Form submission is the browser process of sending the values of an HTML form for processing and potentially navigating or reloading the page.

Form submission is the browser process of collecting values from an HTML form and sending them to the form’s target URL. A button inside a form submits it by default, which can reload the current page even when no target URL or inputs are specified. section 8.4.1

Handle submission in React by:

  • Attach the handler to the form’s onSubmit event rather than only to the button’s onClick event. section 8.4.2
  • Call evt.preventDefault() to stop the browser’s native submission and page reload. section 8.4.2
  • Read controlled-input values from component state, then send them to a service or storage location. section 9.2.4
  • Use the form submit event to support both clicking Submit and pressing Enter while an input has focus. section 9.2.4

React form handler

const onSubmit = (evt) => { evt.preventDefault(); handleAdd(data); }; return <form onSubmit={onSubmit}>...</form>; A to-do form stores title, category, and date in state, passes the collected object to its parent, and prevents the browser reload. section 9.2.4

Listing 8.8 Admin form (potentially fixed?)
import { useState } from "react";
function Admin() {
  const [password, setPassword] = useState("");
  const [isAdmin, setAdmin] = useState(false);
  const onSubmit = (evt) => {                   #1
    evt.preventDefault();                       #2
    if (password === "platypus") {
      setAdmin(true);
    }
  };
  return isAdmin ? (
    <h1>Bacon is delicious!</h1>
  ) : (
    <form onSubmit={onSubmit}>                  #3
      <input
        type="password"
        onChange={(evt) => setPassword(evt.target.value)}
      />
      <button>Login</button>
    </form>
  );
}
function App() {
  return <Admin />;
}
export default App;

Browse a complete React admin form showing onSubmit, preventDefault, password state, and conditional rendering. section 8.4.2

How React Quickly, Second Edition covers it

How deep it goes
Explains browser defaults, submit-handler placement, preventDefault, and Enter-key behavior, but defers proper web security.
Examples
Uses a password admin form and a controlled to-do form that adds title, category, and date values to a list.
What it assumes
Assumes familiarity with HTML forms and introduces React submit handlers, controlled state, and event prevention incrementally.
What you can do after
Apply via React by handling clicks and Enter-key submissions, preventing reloads, and passing controlled form data to application logic.
How it connects
Connects form submission with event handling, controlled inputs, component state, preventDefault, and parent-child data flow.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React Quickly, Second Edition. The section links point to the parts of the book it draws on.