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.SyntaxonClick={handleClick}ParameterThe callback function handleClick which is invoked when onClick event is triggeredReturn TypeIt 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 onClick EventExample 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 to Create Counter Comment More infoAdvertise with us Next Article Company Preparation A ashishbhardwaj18 Follow Improve Article Tags : Web Technologies ReactJS React Events Similar Reads Interview PreparationInterview Preparation For Software DevelopersMust Coding Questions - Company-wise Must Do Coding Questions - Topic-wiseCompany-wise Practice ProblemsCompany PreparationCompetitive ProgrammingSoftware Design-PatternsCompany-wise Interview ExperienceExperienced - Interview ExperiencesInternship - Interview ExperiencesPractice @GeeksforgeeksProblem of the DayTopic-wise PracticeDifficulty Level - SchoolDifficulty Level - BasicDifficulty Level - EasyDifficulty Level - MediumDifficulty Level - HardLeaderboard !!Explore More...Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvance Data StructuresMatrixStringAll Data StructuresAlgorithmsAnalysis of AlgorithmsSearching AlgorithmsSorting AlgorithmsPattern SearchingGeometric AlgorithmsMathematical AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide & ConquerBacktrackingBranch & BoundAll AlgorithmsProgramming LanguagesCC++JavaPythonC#Go LangSQLPHPScalaPerlKotlinWeb TechnologiesHTMLCSSJavaScriptBootstrapTailwind CSSAngularJSReactJSjQueryNodeJSPHPWeb DesignWeb BrowserFile FormatsComputer Science SubjectsOperating SystemsDBMSComputer NetworkComputer Organization & ArchitectureTOCCompiler DesignDigital Elec. & Logic DesignSoftware EngineeringEngineering MathematicsData Science & MLComplete Data Science CourseData Science TutorialMachine Learning TutorialDeep Learning TutorialNLP TutorialMachine Learning ProjectsData Analysis TutorialTutorial LibraryPython TutorialDjango TutorialPandas TutorialKivy TutorialTkinter TutorialOpenCV TutorialSelenium TutorialGATE CSGATE CS NotesGate CornerPrevious Year GATE PapersLast Minute Notes (LMNs)Important Topic For GATE CSGATE CoursePrevious Year Paper: CS exams Like