Back to Blog
Lesson 19 of the React Fundamentals: Build Modern UIs from Scratch course
ReactJune 25, 20263 min read

Handling Form Submissions: Preventing Page Reloads in React

Learn how to manage form handling in React by using onSubmit events and preventing default browser reloads to build seamless, interactive user interfaces.

reactjavascriptfrontend

Previously in this course, we explored handling click events to make our application responsive to user interaction. While onClick is perfect for buttons, gathering complex user data requires a more robust approach. In this lesson, we’ll move from individual clicks to form handling, focusing on how to capture user input and manage submissions without triggering the browser's default page-refresh behavior.

Understanding Form Submission from First Principles

In traditional web development, submitting an HTML <form> triggers a browser-level request. The browser attempts to navigate to the URL specified in the action attribute, which usually causes the entire page to reload.

In a modern React application, we want to intercept this event. We don't want the page to refresh; instead, we want to capture the data, process it (perhaps by filtering our movie list), and update our application state—all while keeping the user on the current screen. This is the cornerstone of building a "Single Page Application" (SPA) feel.

The onSubmit Handler and preventDefault

React provides the onSubmit event, which attaches to the <form> element itself. When a user presses "Enter" in an input field or clicks a submit button, this event fires. However, because the browser's default behavior is to attempt a form submission to a server, we must explicitly tell it to stop.

We do this using the event.preventDefault() method. This is a standard Web API method that tells the browser: "I’ve got this covered; don't perform your default action."

Concrete Example: Adding a Search Form

Let's integrate a simple search form into our movie-browser project. We'll create a SearchForm component that logs the submission to the console.

JSX
import { useState } from CE9178">'react';

function SearchForm() {
  const [query, setQuery] = useState("");

  const handleSubmit = (event) => {
    // 1. Stop the browser from reloading the page
    event.preventDefault();
    
    // 2. Now we can safely use the query state
    console.log("Searching for:", query);
  };

  return (
    <form onSubmit={handleSubmit}>
      <input 
        type="text" 
        value={query} 
        onChange={(e) => setQuery(e.target.value)} 
        placeholder="Search movies..."
      />
      <button type="submit">Search</button>
    </form>
  );
}

export default SearchForm;

In this example, notice that the handleSubmit function receives the event object automatically. By calling event.preventDefault() as the very first line, we ensure that no matter how the user triggers the submission, the page stays put.

Hands-on Exercise

It's your turn to apply this to our movie project.

  1. Create a new file named MovieSearch.jsx.
  2. Implement a form with a text input and a submit button.
  3. Add an onSubmit handler that prevents the default reload.
  4. Inside the handler, log a message to the console: "Form submitted with [your state value]".
  5. Import and add this component to your main App.jsx layout.

Common Pitfalls

Even experienced developers run into these minor issues with form handling:

  • Forgetting preventDefault(): If your page flashes or clears suddenly when you click submit, you likely forgot to prevent the default action. It's the most common "why is my state disappearing?" bug.
  • Button Types: If you put a <button> inside a form without specifying type="button", browsers default it to type="submit". If you have a button that shouldn't submit the form (like a "Clear" button), always explicitly set type="button".
  • Missing onChange: If your input is tied to state but you don't provide an onChange handler, the input will appear "stuck" and read-only. We will dive deeper into the nuances of this in our upcoming lesson on controlled components.

Recap

Form handling in React revolves around two main pillars: attaching an onSubmit handler to the form tag and using event.preventDefault() to stop the browser's native reload behavior. By doing this, we keep the application state intact and provide a smooth, fast experience for our users. You now have the foundation needed to turn static inputs into functional, interactive tools.

Up next: We will refine our approach by mastering Controlled Components, where we fully synchronize our form inputs with React state.

Similar Posts