React onClick Event Last Updated : 29 Nov, 2024 Suggest changes Share 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 A ashishbhardwaj18 Follow Article Tags : Web Technologies ReactJS React Events Explore React Tutorial 5 min read React FundamentalsReact Introduction 6 min read React Environment Setup 3 min read React JS ReactDOM 2 min read React JSX 5 min read ReactJS Rendering Elements 3 min read React Lists 4 min read React Forms 4 min read ReactJS Keys 4 min read Components in ReactReact Components 4 min read ReactJS Functional Components 4 min read React Class Components 3 min read ReactJS Pure Components 4 min read ReactJS Container and Presentational Pattern in Components 2 min read ReactJS PropTypes 5 min read React Lifecycle 7 min read React HooksReact Hooks 8 min read React useState Hook 5 min read ReactJS useEffect Hook 5 min read Routing in ReactReact Router 5 min read React JS Types of Routers 10 min read Advanced React ConceptsLazy Loading in React and How to Implement it ? 4 min read ReactJS Higher-Order Components 5 min read Code Splitting in React 4 min read React ProjectsCreate ToDo App using ReactJS 3 min read Create a Quiz App using ReactJS 4 min read Create a Coin Flipping App using ReactJS 3 min read How to create a Color-Box App using ReactJS? 4 min read Dice Rolling App using ReactJS 4 min read Guess the number with React 3 min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like