Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • 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:
React onClick Event
Next article icon

React onClick Event

Last Updated : 29 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The onClick event in React is used for handling a function when an element, such as a button, div, or any clickable element, is clicked.

Syntax

onClick={handleClick}

Parameter

  • The callback function handleClick which is invoked when onClick event is triggered

Return Type

  • It is an event object containing information about the methods and is triggered when the mouse is clicked.

Example 1: In this React code, the button text and heading message toggle on click using state changes.

CSS
/* Write CSS Here */  .App {     display: flex;     flex-direction: column;     justify-content: center;     align-items: center;  }  body {     background-color: antiquewhite; }  .App>h2 {     text-align: center; }  .App>button {     width: 8rem;     font-size: larger;     padding: 2vmax auto;     height: 1.8rem;     color: white;     background-color: rgb(34, 34, 33);     border-radius: 10px; }  button:hover {     background-color: rgb(80, 80, 78);  } 
JavaScript
// App.js  import "./App.css"  import React, {useState} from "react";  const App = () => {     const [num, setNum] = useState(false);     const [btn, setBtn] = useState(false);     const handleClick = () => {         setNum(!num);         setBtn(!btn);     };      return (         <div className="App">             <h2> {                 num ?                     "onClick event performed" :                     "onClick event not performed"             }             </h2>             <button onClick={handleClick}>                 {btn ? "clicked" : "onClick"}             </button>         </div>     ); };  export default App; 

Output:

React onClickevent Example GIF
React onClick Event

Example 2: In this React code, each time the button is clicked, it increments a number by 1 and displays the updated value on the screen.

CSS
/* Write CSS Here */  .App {     display: flex;     flex-direction: column;     justify-content: center;     align-items: center; }  body {     background-color: antiquewhite; }  .App>h2 {     text-align: center; }  .App>button {     width: 8rem;     font-size: larger;     padding: 2vmax auto;     height: 1.8rem;     color: white;     background-color: rgb(34, 34, 33);     border-radius: 10px; }  button:hover {     background-color: rgb(80, 80, 78);  } 
JavaScript
// App.js  import "./App.css"  import React, {useState} from "react";  const App = () => {     const [num, setNum] = useState(0);     const handleClick = () => {         setNum(num + 1);     };      return (         <div className="App">             <h2> {num}</h2>             <button onClick={handleClick}>                 Add one             </button>         </div>     ); };  export default App; 

Output:

React onClick event eample 2 GIF
React onClick Event to Create Counter

Next Article
React onClick Event

A

ashishbhardwaj18
Improve
Article Tags :
  • Web Technologies
  • ReactJS
  • React Events

Similar Reads

    React onDoubleClick Event
    The onDoubleClick event in React is a native DOM event that triggers when the user double-clicks on an element, typically using the left mouse button. This event is part of a group of mouse events that React handles, including onClick, onMouseDown, onMouseUp, and others.onDoubleClick occurs when a m
    5 min read
    React onBlur Event
    In React, handling user interactions efficiently is important for building responsive and dynamic applications. One of the commonly used events for managing focus behaviour is the onBlur event. This event is important for tracking when a user moves the focus away from an element, such as an input fi
    6 min read
    React onKeyUp Event
    React onKeyUp is an event listener that is used to detect the key release event in a browser using JavaScript. This event occurs once after the key is pressed and released.It is similar to the HTML DOM onkeyup event but uses the camelCase convention in React.Syntax:<input onKeyUp={keyUpFunction}/
    2 min read
    React onKeyDown Event
    React onKeyDown event occurs on a key press event in a browser. onKeyDown is an updated event in place of onKeyPress. onKeyPress is now deprecated because it does not work for all keys (like CTRL, SHIFT, and ALT) in all browsers, so onKeyDown is a new event listener that is used to detect the key pr
    2 min read
    React onTouchEnd Event
    React onTouchEnd event event fires when the user touches screen and releases the touch. Similar to other elements in it, we have to pass a function for process execution.It is similar to the HTML DOM ontouchend event but uses the camelCase convention in React.Syntax:onTouchEnd={function}Parameter :
    2 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