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

Passing Arguments to Event Handlers

Learn how to pass values such as student names, course names, and IDs to React event handler functions.

1. What Are Arguments in Event Handlers?

An argument is a value passed to a function when it is called. In React, we sometimes need to pass extra information to an event handler.

For example, when a student clicks a button, we may want to display that student's name. We can pass the name as an argument to the handler.

Simple Example:
StudentMessage.jsx JSX
function StudentMessage() {
    function showMessage(name) {
        alert("Hello, " + name);
    }

    return (
        <button onClick={() => showMessage("SAMADHAN")}>
            Show Message
        </button>
    );
}

export default StudentMessage;

Explanation: The arrow function waits for the click and then passes "SAMADHAN" to showMessage().

Expected Output
Hello, SAMADHAN

2. Passing a Single Argument

We can pass one value to an event handler by using an arrow function inside the event prop.

CourseHandler.jsx JSX
function CourseHandler() {
    function showCourse(courseName) {
        alert("Selected Course: " + courseName);
    }

    return (
        <button
            onClick={() => showCourse("React Development")}>
            View Course
        </button>
    );
}

export default CourseHandler;

Simple Explanation

  1. showCourse(courseName) receives a course name.
  2. The arrow function passes "React Development".
  3. The function runs when the button is clicked.
  4. An alert displays the selected course.
Interactive Output
Selected Course: React Development

3. Passing Multiple Arguments

An event handler can receive multiple arguments. For example, we can pass both a student's name and their course name.

StudentCourse.jsx JSX
function StudentCourse() {
    function showDetails(name, course) {
        alert(name + " selected " + course);
    }

    return (
        <button
            onClick={() =>
                showDetails("SAMIKSHA", "Java Development")
            }>
            Show Details
        </button>
    );
}

export default StudentCourse;
Simple Explanation: The function receives two values: name and course. The arrow function passes both values when the button is clicked.
Expected Output
SAMIKSHA selected Java Development

4. Passing an ID to an Event Handler

In real applications, we often pass an ID to identify a particular record. For example, a course ID can be passed when a user clicks a View Course button.

CourseDetails.jsx JSX
function CourseDetails() {
    function showCourseId(id) {
        alert("Selected Course ID: " + id);
    }

    return (
        <button onClick={() => showCourseId(101)}>
            View Course
        </button>
    );
}

export default CourseDetails;

Explanation: The number 101 is passed to showCourseId(). The function displays the received ID.

Expected Output
Selected Course ID: 101

5. Passing Arguments with an Input Value

We can also pass a value entered by the user to an event handler. In this example, the user enters a name and clicks a button to display a greeting.

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

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

    function showGreeting(studentName) {
        alert("Welcome, " + studentName);
    }

    return (
        <div>
            <input
                value={name}
                onChange={(event) => setName(event.target.value)}
                placeholder="Enter your name"
            />

            <button onClick={() => showGreeting(name)}>
                Greet Student
            </button>
        </div>
    );
}

export default Greeting;
Simple Explanation: The input value is stored in name. When the button is clicked, name is passed to showGreeting().
Interactive Output
Your greeting will appear here.

6. Difference Between Function Reference and Arrow Function

Function Reference

function handleClick() {
    alert("Button clicked!");
}

<button onClick={handleClick}>
    Click Me
</button>

Use this when you do not need to pass additional arguments.

Arrow Function

function showName(name) {
    alert(name);
}

<button onClick={() => showName("PRADNYA")}>
    Show Name
</button>

Use this when you want to pass arguments or perform additional work when the event occurs.

Important: Avoid writing onClick={showName("PRADNYA")} for normal click handling because it calls the function during rendering. Use onClick={() => showName("PRADNYA")} instead.

7. Common Mistakes

1. Calling the function immediately

// Incorrect for normal click handling
<button onClick={showMessage("SAM")}>
    Click
</button>

// Correct
<button onClick={() => showMessage("SAM")}>
    Click
</button>

2. Forgetting to pass the required argument

function showCourse(courseName) {
    alert(courseName);
}

// Pass the course name
<button onClick={() => showCourse("React")}>
    View Course
</button>

3. Confusing an event object with a custom argument

// React provides the event object
<button onClick={handleClick}>
    Click
</button>

// Custom argument
<button onClick={() => showStudent(101)}>
    View Student
</button>

The event object contains event information. A custom argument is a value that we choose to pass to the function.

8. Practice Questions

  1. What is an argument in a function?
  2. Why do React event handlers sometimes need arguments?
  3. How do you pass a student's name to a click handler?
  4. How do you pass multiple arguments to a function?
  5. Why do we use an arrow function when passing arguments?
  6. Write a component that displays a course ID when clicked.
  7. Create an input that greets the entered student name.

Summary

React event handlers can receive arguments such as names, course titles, IDs, and input values. Use an arrow function inside an event prop to pass those values when an event occurs. A function reference is suitable when no custom argument is needed. Passing arguments correctly makes event handlers more flexible and useful.