How to pass a parameter to an event handler or callback ?
Last Updated :
03 Nov, 2023
In React, to pass a parameter to an event handler or callback simply means to execute that function or code when the event is triggered. It links the events to the respective functions.
Prerequisites:
Approach
We will define a function to create a window alert and link it to the click event using an event handler. When the event triggers the corresponding function will be executed creating an alert on the browser screen.
Steps to create React Project
Step 1: Use this command in the terminal to initialize a react project.
npx create-react-app project_name
Step 2: After creating your react project move into the folder to perform different operations.
cd project_name
Project Structure: 
Example 1:This example uses an event handler that sends a Welcome message i.e. Alert whenever you click the button.Â
JavaScript
// Filename - App.js
import React from "react";
class App extends React.Component {
call() {
alert("Welcome to Geeks for Geeeks!");
}
render() {
return (
<div>
<h3>React Example for event handlers</h3>
<button onClick={this.call}>
Click the button!
</button>
</div>
);
}
}
export default App;
Step to run the application: Open the terminal and type the following command.Â
npm start
Output: This output will be visible on http://localhost:3000/ on browser window.

Example 2: This example uses arrow funciton to execute the alert as the event handler and show message
JavaScript
// Filename - App.js
import React from "react";
class App extends React.Component {
call(message, event) {
alert(message);
}
render() {
return (
<div>
<h3>React Example for event handler</h3>
{
//Creating a arrow function
}
<button
onClick={(event) =>
this.call("Your message", event)
}
>
Click the button!
</button>
</div>
);
}
}
export default App;
Step to run the application: Open the terminal and type the following command.Â
npm start
Output: This output will be visible on http://localhost:3000/ on browser window.
Example 3: In this example, we will use Bind Method that is also used to pass the parameters in functions of class-based components.
JavaScript
// Filename - App.js
import React from "react";
class App extends React.Component {
call(message) {
alert(message);
}
render() {
return (
<div>
<h3>React Example for event handlers</h3>
<button
onClick={this.call.bind(
this,
"Your message"
)}
>
Click the button!
</button>
</div>
);
}
}
export default App;
Step to run the application: Open the terminal and type the following command.Â
npm start
Output: This output will be visible on http://localhost:3000/ on browser window.
Similar Reads
How to bind an event handler in JSX callback ? ReactJS is a JavaScript library focused on building user interfaces. JSX callbacks in React are used to associate event handlers with elements in JSX code. Event handlers, like those for button clicks or form submissions, are functions triggered by events. In React, event handlers are bound to eleme
4 min read
How to pass two parameters to EventEmitter in Angular 9 ? In Angular, we can transmit the data in both directions i.e. inside: to the child component and outside: to the parent component. For sending data to the child component, we use property binding and for the latter we use EventEmitter. In this article, we will talk about the EventEmitter directive an
3 min read
JavaScript Passing parameters to a callback function Callback FunctionPassing a function to another function or passing a function inside another function is known as a Callback Function. In other words, a callback is an already-defined function that is passed as an argument to the other code Syntax:function geekOne(z) { alert(z); }function geekTwo(a,
2 min read
How to Pass Parameters to on:click in Svelte? Parameters are the data that you want to pass to a function when an event, such as a click, occurs. In Svelte, you can pass parameters to the on:click event handler in a couple of ways, either directly within the event handler or by using element references.In this article, we will explore two diffe
3 min read
Which is first argument typically passed to a Node.js callback handler ? A callback handler function in Node.js is a way to handle something after a particular operation is completed. It is one of the ways to handle asynchronous code which takes a long time to yield the result so we can call the callback handler with the error if any, and the result of the asynchronous o
3 min read