Master React From Scratch

Clear, practical, and structured React lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to React
Introduction to React What Is React? Features of React Advantages of React Limitations of React Uses of React React vs JavaScript React Architecture
2. React Getting Started
Getting Started Introduction Prerequisites Installing Node.js and npm Creating a React Project with Vite React Project Structure Running React Application Your First React Application
3. JSX
JSX Introduction What Is JSX? JavaScript Expressions JSX Attributes Rendering JSX Elements JSX Children React Fragments JSX Rules and Best Practices JSX vs HTML
4. React Components
Components Introduction Functional Components Class Components Component Composition Reusable Components Component Tree Component Lifecycle Import and Export Components
5. React Props
Props Introduction What Are Props? Passing Props to Components Props Destructuring Default Props Values Children Props Typing Props with TypeScript Props vs State
6. React State
State Introduction What Is State? useState Hook Updating State Functional State Updates State with Objects State with Arrays State Immutability Lifting State Up
7. React Events
Events Introduction Handling Events Passing Arguments to Event Handlers Synthetic Events Form Events Keyboard and Mouse Events Event Propagation
8. Conditional Rendering
Conditional Rendering Introduction If-Else Rendering Ternary Operator Logical AND Operator Element Variables Preventing Rendering Multiple Conditions Conditional CSS Classes
9. Lists and Keys
Lists and Keys Introduction Rendering Lists with map() Understanding Keys Choosing Unique Keys Lists of Objects Filtering Lists Sorting Lists Empty Lists and Conditional Rendering
10. React Forms
Forms Introduction Handling Forms Controlled Components Uncontrolled Components Handling Input Fields Checkboxes and Radio Buttons Select and Textarea Form Validation Form Submission
11. React Hooks
Hooks Introduction useState Hook useEffect Hook Effect Dependencies and Cleanup useRef Hook useReducer Hook useMemo Hook useCallback Hook Custom Hooks Rules of Hooks
12. React Context API
Context API Introduction createContext useContext Context Provider Reading Context Updating Context Using Multiple Contexts Context API vs Props Context API Best Practices
13. React Routing
Routing Introduction Introduction to React Router Installing React Router BrowserRouter Setup Routes and Route Components Link and NavLink Programmatic Navigation URL Parameters and useParams Nested Routes Not Found Routes Protected Routes
14. API Integration
API Integration Introduction Fetch API Async/Await with API Calls Displaying API Data Loading and Error States Sending POST Requests API Error Handling Using Axios Custom Fetch Hook API Environment Variables
15. State Management
State Management Introduction Global State Prop Drilling Introduction to Redux Redux Core Concepts Redux Toolkit Store and Slices useSelector and useDispatch Async Operations with Redux Toolkit State Management Best Practices
16. React Performance
Performance Introduction Understanding Re-Renders Avoiding Unnecessary Re-Renders React.memo useMemo Optimization useCallback Optimization React Developer Tools and Profiler Lazy Loading Components Code Splitting Optimizing Large Lists Performance Best Practices

Handling Form Submission in React

Learn how to submit forms in React, handle form data, prevent page reloads, and display success messages after successful submission.

1. What Is Form Submission?

Form submission is the process of processing information entered by a user through a form. For example, a student registration form can collect a student's name, email address, and selected course.

React uses the onSubmit event to handle form submission. When the user submits a form, the associated event handler executes and processes the entered values.

Important: Use event.preventDefault() when handling submission in React without triggering the browser's default form submission and page reload.

2. How Form Submission Works

A typical form submission process follows these steps:

Enter Data
→
Click Submit
→
Run Handler
→
Display Result

The user enters information into the form fields. When the form is submitted, React calls the submit handler. The handler processes the values and updates the component's state to display the result.

3. Basic Form Submission Example

In this example, the user enters a name and clicks the Submit button. React displays a welcome message.

Example Code

import { useState } from "react";

function App() {
    const [name, setName] = useState("");
    const [message, setMessage] = useState("");

    function handleSubmit(event) {
        event.preventDefault();

        setMessage(`Welcome, ${name}!`);
    }

    return (
        <div>
            <h2>Student Registration</h2>

            <form onSubmit={handleSubmit}>
                <label>Enter Name: </label>

                <input
                    type="text"
                    value={name}
                    onChange={(event) =>
                        setName(event.target.value)
                    }
                    required
                />

                <button type="submit">
                    Submit
                </button>
            </form>

            <p>{message}</p>
        </div>
    );
}

export default App;

Expected Output

Student Registration

Enter Name: Samadhan

Button: Submit


Welcome, Samadhan!
Explanation: The handleSubmit() function executes when the form is submitted. The preventDefault() method prevents the default browser behavior, and setMessage() updates the displayed message.

4. Handling Multiple Form Fields

A form may contain multiple input fields. We can use React state to store each input value and process all the values when the user submits the form.

Example Code

import { useState } from "react";

function App() {
    const [name, setName] = useState("");
    const [email, setEmail] = useState("");
    const [result, setResult] = useState("");

    function handleSubmit(event) {
        event.preventDefault();

        setResult(
            `Name: ${name}, Email: ${email}`
        );
    }

    return (
        <div>
            <h2>Student Details</h2>

            <form onSubmit={handleSubmit}>
                <input
                    type="text"
                    placeholder="Enter name"
                    value={name}
                    onChange={(event) =>
                        setName(event.target.value)
                    }
                    required
                />

                <br /><br />

                <input
                    type="email"
                    placeholder="Enter email"
                    value={email}
                    onChange={(event) =>
                        setEmail(event.target.value)
                    }
                    required
                />

                <br /><br />

                <button type="submit">
                    Submit Details
                </button>
            </form>

            <p>{result}</p>
        </div>
    );
}

export default App;

Expected Output

Name: Samiksha

Email: samiksha@example.com

After clicking Submit Details:

Name: Samiksha, Email: samiksha@example.com
Explanation: The name and email state variables store the input values. The submit handler reads these values and displays them after submission.

5. Important Points About Form Submission

  • Use onSubmit to handle form submission.
  • Use event.preventDefault() to prevent the browser's default form submission behavior.
  • Use type="submit" for the form submission button.
  • Use React state to store and display form values.
  • Use required to make a field mandatory through basic browser validation.
  • Use an API or backend when submitted data must be saved to a server or database.
Remember: Displaying form values on the screen does not automatically save the data to a database. Server-side processing is required to persist the data.

6. Practice Tasks

Practice the following tasks on your own:

  1. Create a form that accepts a student's name and displays a welcome message after submission.
  2. Create a form with name, email, and phone number fields. Display all the values after submission.
  3. Create a course registration form with student name and course name.
  4. Display a success message after the form is submitted.
  5. Make the name and email fields mandatory.
  6. Create a form that displays the submitted details without reloading the page.

Summary

Form submission in React allows developers to process user input through the onSubmit event. The event handler can use event.preventDefault() to prevent the browser's default page reload, while React state stores input values and displays the submitted result. The required attribute provides basic browser validation for mandatory fields, and an API or backend can process and save data when necessary. These concepts help developers create interactive registration forms and other data-entry forms at CIIT Training Institute.