Lesson 19/100

Tutorials React.js Tutorial

Form Submit — Complete Guide

Form Submit — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of React.js Tutorial on Toolliyo Academy.

On this page

React.js Tutorial · Lesson 19 of 100

Form Submit

BeginnerIntermediateAdvancedProfessional

Beginner · 1 — Learn by example · ~6 min · Module 2: Props, Events & Lists

What is this?

Form submit in React uses onSubmit on the

and e.preventDefault() to stop the browser from reloading the page. You read field values from state.

Why should you care?

Login, signup, and checkout all submit without a full page refresh — that is standard SPA behavior.

See it live — copy this example

Paste into src/App.jsx (or the file noted in the steps). Save and watch the browser update.

import { useState } from 'react';

function LoginForm() {
  const [email, setEmail] = useState('');

  function handleSubmit(e) {
    e.preventDefault();
    console.log('Login with', email);
  }

  return (
    <form onSubmit={handleSubmit}>
      <input value={email} onChange={e => setEmail(e.target.value)} />
      <button type="submit">Login</button>
    </form>
  );
}

Run Example »

Edit the code and click Run to see the result update live.

Code
Result

What happened?

  • onSubmit fires when user clicks submit or presses Enter.
  • preventDefault stops navigation.
  • Read state variables you built with onChange.

Try it yourself

  1. Wire onSubmit with preventDefault.
  2. console.log state on submit.
  3. Add alert(JSON.stringify(formData)) to see all fields.
  4. Change text or labels in the example and save — watch the browser update.
  5. Break the code on purpose (remove a bracket), read the error message, then fix it.

Remember

onSubmit + preventDefault on forms. Controlled inputs hold values in state. Submit handler reads state, calls API.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Mid PDF Detailed
Console Logs and performance.now():?
Short answer: Log the time it takes for certain parts of your app to render and pinpoint slow areas. Real-world example (ShopNest) Heavy product grids memoize row components so typing in the search box does not re-render…
Mid PDF Detailed
How do you handle forms in React?
Short answer: Use controlled components and onChange handlers. ✅ Example code function Form() { const [name, setName] = useState(&quot;&quot;); const handleSubmit = e =&gt; { e.preventDefault(); console.log(name); }; ret…
Mid PDF Detailed
How does useMemo optimize performance?
Short answer: useMemo memoizes a computed value to avoid recalculating unless dependencies change. ✅ Syntax: const memoizedValue = useMemo(() =&gt; computeExpensiveValue(a, b), [a, b]); ✅ Use case: Avoid recalculating he…
Mid PDF Detailed
How does useMemo optimize performance?
Short answer: void recalculating heavy logic on every render. Explain a bit more const filteredList = useMemo(() =&gt; { return items.filter(item =&gt; item.includes(searchTerm)); }, [items, searchTerm]); void recalculat…
Mid PDF Detailed
React Developer Tools:?
Short answer: Use the Profiler tab to measure the render times and identify slow components. Check for unnecessary re-renders and optimize with React.memo or shouldComponentUpdate. Real-world example (ShopNest) ShopNest’…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

React.js Tutorial
Course syllabus

React.js Tutorial

Module 1: React Basics & Setup
Module 2: Props, Events & Lists
Module 3: Forms & Hooks
Module 4: Routing & Data
Module 5: State & Authentication
Module 6: Architecture & React 19
Module 7: Performance
Module 8: Full-Stack & Real-Time
Module 9: Testing & Deployment
Module 10: ShopCart Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details