Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • React Tutorial
  • React Exercise
  • React Basic Concepts
  • React Components
  • React Props
  • React Hooks
  • React Router
  • React Advanced
  • React Examples
  • React Interview Questions
  • React Projects
  • Next.js Tutorial
  • React Bootstrap
  • React Material UI
  • React Ant Design
  • React Desktop
  • React Rebass
  • React Blueprint
  • JavaScript
  • Web Technology
Open In App
Next Article:
Callbacks and Events in NodeJS
Next article icon

How to bind an event handler in JSX callback ?

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 elements in JSX using the on syntax (e.g., onClick for click events), and a callback function is assigned to handle the event. This callback function is typically defined using bind() or the arrow function.

Syntax:

<element onEvent={this.eventHandler} />

We will see how to bind the event handler in JSX using the below examples:

Table of Content

  • Binding a click event handler to a button in JSX:
  • Using constructor and bind method.
  • Using arrow function:

Steps to Create the React Application And Installing Module:

Step 1: Create a React application using the following command:

npx create-react-app "foldername"

Step 2: After creating your project folder i.e. folder name, move to it using the following command:

cd foldername

Step 3: After setting up react environment on your system, we can start by creating an App.js file and create a directory by the name of components in which we will write our desired function.

Project Structure:

How to bind an event handler in JSX callback ?
How to bind an event handler in JSX callback?

The updated dependencies in package.json file will look like:

  "dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
}

Approach 1: Binding a click event handler to a button in JSX:

In this approach, we have defined a handleClick method that will log a message to the console when the button is clicked. We have then passed the handleClick method as a callback to the onClick event of the button.

Example: Below is the implementation of the above mentioned approach.

JavaScript
import React from "react"; class App extends React.Component {     handleClick() {         console.log("Button clicked");     }      render() {         return (             <div>                 <h1                     style={{                         color: "green",                         fontSize: "3rem",                         fontWeight: "bold",                     }}                 >                     GeekforGeeks                 </h1>                 <button                     style={{                         fontSize: "3rem",                         fontWeight: "bold",                         border: "2px solid black",                     }}                     onClick={this.handleClick}                 >                     Click Me!                 </button>             </div>         );     } } export default App; 

Step to Run Application: Open the terminal and type the following command.

npm start

Output:

How to bind an event handler in JSX callback ?
How to bind an event handler in JSX callback?

Approach 2: Using constructor and bind method.

This React class component named "App" includes a button with the label "Click Me!" and a header displaying "GeekforGeeks." The button is linked to a "handleClick" method, which logs "Button clicked" to the console when the button is clicked.

Example: This example implements the above mentioned approach.

JavaScript
import React from "react"; class App extends React.Component {     constructor(props) {         super(props);         this.handleClick = this.handleClick.bind(this);     }      handleClick() {         console.log("Button clicked");     }      render() {         return (             <div>                 <h1                     style={{                         color: "green",                         fontSize: "3rem",                         fontWeight: "bold",                     }}                 >                     GeekforGeeks                 </h1>                 <button                     style={{                         fontSize: "3rem",                         fontWeight: "bold",                         border: "2px solid black",                     }}                     onClick={this.handleClick}                 >                     Click Me!                 </button>             </div>         );     } } export default App; 

Step to Run Application: Open the terminal and type the following command.

npm start

Output:

How to bind an event handler in JSX callback ?
How to bind an event handler in JSX callback?

Approach 3: Using arrow function:

The React class component "App" features a button labeled "Click Me!" linked to a handleClick method, logging "Button clicked" when clicked. The component also displays a styled header with the text "GeekforGeeks."

Example: This example implements the above-mentioned approach

JavaScript
import React from "react"; class App extends React.Component {     handleClick = () => {         console.log("Button clicked");     };     render() {         return (             <div>                 <h1                     style={{                         color: "green",                         fontSize: "3rem",                         fontWeight: "bold",                     }}                 >                     GeekforGeeks                 </h1>                 <button                     style={{                         fontSize: "3rem",                         fontWeight: "bold",                         border: "2px solid black",                     }}                     onClick={this.handleClick}                 >                     Click Me!                 </button>             </div>         );     } } export default App; 

Step to Run Application: Open the terminal and type the following command.

npm start

Output:

How to bind an event handler in JSX callback ?
How to bind an event handler in JSX callback?



Next Article
Callbacks and Events in NodeJS
author
ankitjangidx
Improve
Article Tags :
  • Web Technologies
  • ReactJS
  • React-Questions

Similar Reads

  • How to bind event handlers in React?
    Binding event handlers in React is a fundamental concept that ensures interactive and responsive web applications. React, a popular JavaScript library for building user interfaces uses a declarative approach to handle events. This article explores various methods to bind event handlers in React, hig
    3 min read
  • How to pass a parameter to an event handler or callback ?
    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:React JSEvent HandlersReact JS Class ComponentApproachWe will define a function to create a window a
    2 min read
  • Callbacks and Events in NodeJS
    Callbacks and events are fundamental building blocks for asynchronous programming in NodeJS. They're essential for handling operations that might take some time, ensuring your application handles asynchronous operations smoothly. Callback in NodeJSIn NodeJS, Callbacks are functions passed as argumen
    3 min read
  • What is Callback Hell and How to Avoid it in NodeJS?
    In NodeJS, asynchronous programming can lead to Callback Hell, where deeply nested callbacks make the code hard to read and maintain. This happens when multiple asynchronous operations are chained together, creating a complex structure that's difficult to manage. Callback Hell in NodeJSCallback Hell
    6 min read
  • How to call a function on click event in Angular2 ?
    A Function is a set of statements that takes input, does some specific computation, and produces output. An on click event occurs when the user clicks on an element. In this article, we will see How to call a function on click event in Angular2, along with understanding the basic implementation thro
    3 min read
  • How to Create a Custom Callback in JavaScript?
    A callback is a function that executes after another function has been completed in JavaScript. As an event-driven language, JavaScript does not pause for a function to finish before moving on to the next task. Callbacks enable the execution of a function only after the completion of another, making
    3 min read
  • How to Prevent Default Behavior in an Event Callback in React JS ?
    React JS provides events to create interactive and responsive user interfaces. One characteristic of event handling is to prevent the default behavior of events in certain cases. This article covers the process of preventing default behavior in event callbacks within React JS. Prerequisites:Basic Ja
    3 min read
  • How are events handled in React?
    Modern webpages rely on user interactions, triggering events like clicks or keypresses. React facilitates event handling with built-in methods, allowing developers to create listeners for dynamic interfaces and responses. JavaScript events, inherent to the DOM, use bubbling propagation by default, m
    2 min read
  • How to simplify an error callback in ReactJS ?
    This article discusses simplifying error callbacks in React, focusing on handling errors during actions like HTTP requests or JSON data interpretation. While default behavior logs errors in the console, the article advocates for improved user experience by implementing error handling to display user
    3 min read
  • How to get an Element by ID in ReactJS ?
    ReactJS, a popular JavaScript library for user interfaces, empowers developers with tools, such as the ref system, to manipulate the DOM by accessing elements using their unique IDs. This article explores the process of obtaining elements by ID in ReactJS for effective component interaction and mani
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences