Passing Arguments to Event Handlers is an important concept in React JS. It allows developers to send specific values, such as an item ID, username, product name, or index, to a function when a user performs an action like clicking a button. It is commonly used in delete buttons, edit actions, product lists, shopping carts, and other interactive applications.
An event handler is a JavaScript function that runs when a particular event occurs. Events can include clicking a button, typing into an input, submitting a form, or moving the mouse over an element.
React event handlers are assigned through JSX props such as onClick, onChange, and onSubmit. Event handler names use camelCase, and the handler is passed as a function.
Example:
function App() {
function handleClick() {
alert("Button clicked!");
}
return (
<button onClick={handleClick}>
Click Me
</button>
);
}
export default App;
Explanation: The handleClick function runs when the user clicks the button. React receives the function reference and invokes it when the event occurs.
2. What Does Passing Arguments to Event Handlers Mean?
Passing arguments means providing values to a function when an event occurs. These values allow the handler to perform a specific action based on the selected item or user interaction.
Example:
function App() {
function greetUser(name) {
alert("Hello, " + name);
}
return (
<button onClick={() => greetUser("Rahul")}>
Greet User
</button>
);
}
export default App;
Output: When the user clicks the button, an alert displays Hello, Rahul.
Explanation: The arrow function delays the call to greetUser until the click happens and passes "Rahul" as the argument.
3. Why Do We Need to Pass Arguments?
To identify which item a user selects.
To delete a specific record from a list.
To edit a particular product or user.
To pass an ID, name, index, or other data to a function.
To reuse one event handler for multiple buttons.
To control application behavior according to the selected value.
Example Use Cases
Application
Argument
Purpose
Todo application
Task ID
Delete or update a task
E-commerce website
Product ID
Add a product to the cart
User management
User ID
Edit or remove a user
Student management
Student name or ID
Display student details
Image gallery
Image index
Display a selected image
4. Passing Arguments Using an Arrow Function
The most common way to pass an argument to a React event handler is to wrap the function call in an arrow function.
Syntax:
<button onClick={() => handleFunction(argument)}>
Click Me
</button>
Example:
function App() {
function showMessage(message) {
alert(message);
}
return (
<div>
<button onClick={() => showMessage("Welcome to React")}>
Show Message
</button>
<button onClick={() => showMessage("React is easy to learn")}>
Learn React
</button>
</div>
);
}
export default App;
Explanation: Both buttons call the same function but pass different arguments. The first displays Welcome to React, and the second displays React is easy to learn.
5. Passing a String Argument
A string argument is used when a handler needs text, such as a name, message, category, or action label.
Numbers can be passed to event handlers for calculations, quantity updates, pagination, and other numeric operations.
function App() {
function showNumber(number) {
alert("Selected number: " + number);
}
return (
<button onClick={() => showNumber(100)}>
Show Number
</button>
);
}
export default App;
Output:Selected number: 100
7. Passing Multiple Arguments
You can pass two or more arguments to an event handler by including them in the function call inside the arrow function.
Example:
function App() {
function showDetails(name, age) {
alert("Name: " + name + ", Age: " + age);
}
return (
<button onClick={() => showDetails("Amit", 22)}>
Show Details
</button>
);
}
export default App;
Output:Name: Amit, Age: 22
Explanation: The string "Amit" is passed as the first argument, and the number 22 is passed as the second argument.
8. Passing Arguments Along with the Event Object
React passes an event object to the event handler when the event occurs. The event object provides information about the interaction, including the event type and the element that triggered it.
When you need both a custom argument and the event object, explicitly pass the event object from the arrow function.
Example:
function App() {
function handleClick(name, event) {
console.log("Name:", name);
console.log("Event type:", event.type);
}
return (
<button onClick={(event) => handleClick("Priya", event)}>
Click Me
</button>
);
}
export default App;
Explanation: The first argument is "Priya", and the second argument is the click event object. The event object's type property will typically be click.
Important: When using an arrow function, pass the event object explicitly if your handler needs it.
9. Passing Arguments to Event Handlers in Lists
In React, lists are commonly rendered using the map() method. Each list item can have a button that passes its own data to an event handler.
Explanation: When the user clicks Delete, the selected item's ID is passed to deleteItem(). The filter() method creates a new array that excludes that item, and the state update causes React to render the updated list.
11. Passing Arguments to Update State
Arguments can determine how state changes. For example, a counter can increase or decrease depending on the argument passed to a handler.
Explanation: The Increase button passes 1, while the Decrease button passes -1. The updater function calculates the new count from the previous state.
12. Passing Arguments with the bind() Method
JavaScript's bind() method can create a new function with arguments already supplied. It is another way to pass arguments to event handlers, although arrow functions are generally easier to read in modern React function components.
Syntax:
<button onClick={handleClick.bind(null, argument)}>
Click Me
</button>
Example:
function App() {
function showName(name) {
alert("Hello, " + name);
}
return (
<button onClick={showName.bind(null, "Neha")}>
Show Name
</button>
);
}
export default App;
Explanation:bind(null, "Neha") creates a function that supplies "Neha" as the first argument when the button is clicked. In a function component, this approach is usually less straightforward than using an arrow function.
13. Difference Between Passing a Function and Calling a Function
This is one of the most important concepts in React event handling.
Code
Meaning
onClick={handleClick}
Passes the function; React calls it when the event occurs.
onClick={() => handleClick("Amit")}
Passes a new function that calls the handler with an argument when clicked.
onClick={handleClick("Amit")}
Calls the function during rendering instead of waiting for the click; usually incorrect.
onClick={() => alert("Hello")}
Runs the alert when the button is clicked.
onClick={alert("Hello")}
Calls the alert during rendering; incorrect for normal click handling.
Remember: Pass a function to the event prop. When arguments are required, use an arrow function or another function wrapper.
14. Passing Arguments to Child Component Event Handlers
A parent component can define a function and pass it to a child component through props. The child can then call that function with a particular argument.
Explanation: The parent defines handleSelection() and passes it as the onSelect prop. The child invokes that function with "React JS" when the button is clicked.
15. Passing Arguments to Different Event Types
The same approach works with other event handlers, not only onClick.
Explanation: When the selection changes, the handler receives the custom role argument and the event object. In this example, the role label is fixed, while event.target.value provides the selected option value.
16. Arrow Function vs bind()
Feature
Arrow Function
bind()
Example
onClick={() => deleteItem(id)}
onClick={deleteItem.bind(null, id)}
Readability
Usually easy to understand
May be less clear to beginners
Custom arguments
Passed directly in the function call
Preconfigured when creating the bound function
Event object
Pass it explicitly if required
Additional event arguments are supplied after bound arguments
Common use
Modern function components
Useful in some callback and class-component patterns
Recommendation: Prefer arrow functions for most simple argument-passing tasks in React function components.
The incorrect version calls deleteItem(5) during rendering. The correct version waits for the click.
Mistake 2: Forgetting to Pass the Argument
// Incorrect when deleteItem requires an ID
<button onClick={deleteItem}>Delete</button>
// Correct
<button onClick={() => deleteItem(item.id)}>Delete</button>
The first version passes no custom ID. It is valid only if the handler is designed to work without one.
Mistake 3: Confusing the Event Object with a Custom Argument
The handler receives the item ID first and the event object second because the arrow function passes them in that order.
Mistake 4: Using an Array Index as a Stable Identifier
When items can be added, removed, or reordered, prefer a stable unique ID instead of relying on an array index to identify a record.
Mistake 5: Mutating State Directly
When deleting or updating list items, create a new array or object and update state with the appropriate setter instead of directly modifying the existing state.
18. Best Practices
Use arrow functions when passing custom arguments to event handlers.
Use meaningful names such as handleDelete, handleEdit, and handleSelect.
Pass only the arguments that the handler actually needs.
Pass the event object explicitly when the handler needs event details.
Use unique, stable IDs to identify list items.
Use state setters to update data displayed by React.
Use semantic HTML elements such as for button actions.
Avoid unnecessary complex logic directly inside JSX; move it into named handler functions when it improves readability.
Do not optimize every inline arrow function prematurely; consider performance only when there is a demonstrated need.
19. Practical Project: Product List with Select Buttons
Objective: Display a list of products and show the selected product's name and price when the user clicks its button.
The products array stores product IDs, names, and prices.
The map() method displays each product.
Each button passes its product object to handleSelectProduct().
The useState setter stores the selected product.
React displays the selected product details when a product is selected.
20. Interview Questions and Answers
Q1. What does passing arguments to event handlers mean in React?
Answer: It means providing values to a function when an event occurs, allowing the function to perform an action using those values.
Q2. How do you pass an argument to an onClick handler?
Answer: Use an arrow function, such as .
Q3. Why should we use an arrow function to pass arguments?
Answer: It creates a function that invokes the handler with the required arguments when the event occurs, rather than calling the handler during rendering.
Q4. Can we pass multiple arguments to an event handler?
Answer: Yes. For example, onClick={() => handleUser("Amit", 25)} passes two arguments.
Q5. How can we pass the event object and a custom argument together?
Answer: Use onClick={(event) => handleClick(id, event)} and define the handler to accept the arguments in that order.
Q6. What is the difference between onClick={handleClick} and onClick={handleClick()}?
Answer: The first passes the function reference. The second invokes the function immediately during rendering.
Q7. Can we pass an object as an argument?
Answer: Yes. For example, onClick={() => handleProduct(product)} passes the product object.
Q8. What is the purpose of bind()?
Answer: The bind() method creates a new function with specified arguments and, optionally, a particular this value.
Q9. How are arguments used in a list rendered with map()?
Answer: Each item's data can be passed to its handler, for example, onClick={() => handleDelete(item.id)}.
Q10. What is a common real-world use case?
Answer: Passing a product ID to an add-to-cart function or a task ID to a delete function.
21. Practice Exercises
Create a button that passes your name to a greeting function.
Create two buttons that pass different numbers to a calculation function.
Display a list of courses and show the selected course name when its button is clicked.
Create a student list with a View button that passes the student's ID.
Build a todo list with a Delete button that passes the task ID.
Create a product list that passes the complete product object to a selection handler.
Pass a custom ID and the event object to the same event handler.
22. Quick Revision Checklist
Understand React event handlers and JSX event props.
Know how to pass arguments using arrow functions.
Understand the difference between passing and calling a function.
Know how to pass strings, numbers, IDs, and objects.
Know how to pass multiple arguments and the event object.
Understand argument passing inside map().
Know how to update state after an event.
Understand the basic difference between arrow functions and bind().
Passing Arguments to Event Handlers allows React components to respond to user actions using specific data. The most common approach is to wrap the function call in an arrow function, such as onClick={() => handleDelete(item.id)}. This technique is useful for deleting records, selecting products, updating state, handling lists, and building interactive React applications.