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.
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().
2. Passing a Single Argument
We can pass one value to an event handler by using an arrow function inside the event prop.
function CourseHandler() {
function showCourse(courseName) {
alert("Selected Course: " + courseName);
}
return (
<button
onClick={() => showCourse("React Development")}>
View Course
</button>
);
}
export default CourseHandler;
Simple Explanation
showCourse(courseName)receives a course name.- The arrow function passes
"React Development". - The function runs when the button is clicked.
- An alert displays the selected course.
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.
function StudentCourse() {
function showDetails(name, course) {
alert(name + " selected " + course);
}
return (
<button
onClick={() =>
showDetails("SAMIKSHA", "Java Development")
}>
Show Details
</button>
);
}
export default StudentCourse;
name and course.
The arrow function passes both values when the button is clicked.
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.
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.
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.
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;
name.
When the button is clicked, name is passed
to showGreeting().
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.
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
- What is an argument in a function?
- Why do React event handlers sometimes need arguments?
- How do you pass a student's name to a click handler?
- How do you pass multiple arguments to a function?
- Why do we use an arrow function when passing arguments?
- Write a component that displays a course ID when clicked.
- 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.