VOOZH about

URL: https://www.geeksforgeeks.org/reactjs/react-onclick-event/

⇱ React onClick Event - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

React onClick Event

Last Updated : 29 Nov, 2024

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.

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.

Output:

👁 React onClick event eample 2 GIF
React onClick Event to Create Counter
Comment
Article Tags: