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

Form Events in React

Learn how to handle form inputs, track user-entered values, submit forms, and display results using React event handlers.

1. What Are Form Events?

Form events occur when users interact with form elements. For example, users can type in an input field, select an option, or submit a form.

Common React form events include:

  • onChange — handles changes to input values.
  • onSubmit — handles form submission.
  • onFocus — handles when an input receives focus.
  • onBlur — handles when an input loses focus.
Simple Example:
SimpleForm.jsx JSX
function SimpleForm() {
    function handleSubmit(event) {
        event.preventDefault();
        alert("Form submitted!");
    }

    return (
        <form onSubmit={handleSubmit}>
            <input
                type="text"
                placeholder="Enter your name"
                required
            />

            <button type="submit">
                Submit
            </button>
        </form>
    );
}

export default SimpleForm;

Explanation: When the user submits the form, React calls handleSubmit(). preventDefault() stops the browser's normal form submission and page reload.

Interactive Output
Your result will appear here.

2. Handling Input with onChange

The onChange event is used to handle changes in an input field. In React, we can store the entered value in state and display it on the page.

NameInput.jsx JSX
import { useState } from "react";

function NameInput() {
    const [name, setName] = useState("");

    function handleChange(event) {
        setName(event.target.value);
    }

    return (
        <div>
            <input
                type="text"
                value={name}
                onChange={handleChange}
                placeholder="Enter your name"
            />

            <p>Your name: {name}</p>
        </div>
    );
}

export default NameInput;
Simple Explanation:
  • useState("") creates an empty name value.
  • onChange calls handleChange().
  • event.target.value reads the entered text.
  • setName() updates the state.
Interactive Input Preview
Your name:

3. Handling Form Submission with onSubmit

The onSubmit event runs when a form is submitted. We commonly use it to validate the form or process the entered information.

StudentForm.jsx JSX
import { useState } from "react";

function StudentForm() {
    const [studentName, setStudentName] = useState("");
    const [message, setMessage] = useState("");

    function handleSubmit(event) {
        event.preventDefault();
        setMessage("Welcome, " + studentName + "!");
    }

    return (
        <form onSubmit={handleSubmit}>
            <input
                value={studentName}
                onChange={(event) =>
                    setStudentName(event.target.value)
                }
                placeholder="Enter student name"
                required
            />

            <button type="submit">Register</button>

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

export default StudentForm;
Simple Explanation: The form stores the student's name in state. On submission, preventDefault() prevents the normal browser submission, and setMessage() displays a greeting.
Interactive Registration Form
Registration message will appear here.

4. What Is event.preventDefault()?

By default, submitting a normal HTML form can cause the browser to navigate or reload the page. In React, we often want to handle the submission ourselves.

PreventDefault.jsx JSX
function PreventDefault() {
    function handleSubmit(event) {
        event.preventDefault();
        console.log("Default form submission prevented");
    }

    return (
        <form onSubmit={handleSubmit}>
            <button type="submit">
                Submit Form
            </button>
        </form>
    );
}

export default PreventDefault;
Remember: Call event.preventDefault() inside the submit handler when you want to handle submission without the browser's normal form action.
Interactive Example
The page will not reload when you submit this form.

5. Handling Focus and Blur Events

The onFocus event occurs when an input receives focus. The onBlur event occurs when the input loses focus.

FocusBlur.jsx JSX
function FocusBlur() {
    function handleFocus() {
        console.log("Input focused");
    }

    function handleBlur() {
        console.log("Input lost focus");
    }

    return (
        <input
            onFocus={handleFocus}
            onBlur={handleBlur}
            placeholder="Click here"
        />
    );
}

export default FocusBlur;
Simple Explanation: Click inside the input to trigger onFocus. Click somewhere else to trigger onBlur.
Interactive Focus and Blur Preview
Waiting for input interaction...

6. Handling a Select Dropdown

React can handle dropdown changes using onChange. We can read the selected option and display it to the user.

CourseSelect.jsx JSX
import { useState } from "react";

function CourseSelect() {
    const [course, setCourse] = useState("");

    return (
        <div>
            <select
                value={course}
                onChange={(event) =>
                    setCourse(event.target.value)
                }>
                <option value="">Select a course</option>
                <option value="React">React</option>
                <option value="Java">Java</option>
                <option value="Python">Python</option>
            </select>

            <p>Selected course: {course}</p>
        </div>
    );
}

export default CourseSelect;
Interactive Dropdown
Selected course: None

7. Common Form Event Mistakes

1. Forgetting preventDefault()

function handleSubmit(event) {
    event.preventDefault();
    console.log("Form submitted");
}

Prevent the browser's default form submission when handling the form in your React component.

2. Forgetting to connect onSubmit

// Correct
<form onSubmit={handleSubmit}>
    <button type="submit">Submit</button>
</form>

Connect the submit handler to the form element.

3. Forgetting to update input state

function handleChange(event) {
    setName(event.target.value);
}

For a controlled React input, update the state in onChange.

8. Practice Questions

  1. What are form events in React?
  2. What is the purpose of onChange?
  3. How does onSubmit work?
  4. Why do we use event.preventDefault()?
  5. What is the difference between onFocus and onBlur?
  6. Create a form that displays a greeting after submission.
  7. Create a dropdown that displays the selected course.

Summary

Form events help React components respond to user input and form submission. Use onChange to track values, onSubmit to handle submission, and preventDefault() to stop the browser's default form action when necessary. React state can store form values and display the updated results.