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:
What is onPasteCapture Event in ReactJS ?
Next article icon

What is onClickCapture Event in ReactJS?

Last Updated : 19 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The onClickCapture event in React is part of React’s event handling system, and it is used to handle click events during the capture phase of event propagation. It is a variant of the standard onClick event, but it differs when it is triggered in the event lifecycle.

In the event propagation model, events can propagate in two phases

  • Capture Phase: The event starts from the root and propagates down to the target element.
  • Bubbling Phase: The event starts from the target element and propagates up to the root.

Syntax

<button onClickCapture = {function}/>
  • element: The JSX element that will trigger the event (e.g., <button>, <div>, etc.).
  • onClickCapture: The event handler for the capture phase of the click event.
  • handlerFunction: The function that will be called when the event is triggered during the capture phase.

By default, React’s onClick event is handled during the bubbling phase, which means the event is fired when it reaches the target element. However, onClickCapture allows you to handle the event during the capture phase, meaning it is triggered before the event reaches the target element.

Using onClickCapture in React

To use onClickCapture, simply attach it to a React element as you would with onClick.

JavaScript
//App.js  import React from "react";  function App() { 	const onClickCaptureHandler = () => { 		console.log("onClickCapture!"); 	}; 	return ( 		<div className="App"> 			<h1> Hey Geek!</h1> 			<label>Please click on the button</label> 			<button onClickCapture={onClickCaptureHandler}> 				click Me! 			</button> 		</div> 	); }  export default App; 

Output

In this example

  • It uses onClickCapture on the button to demonstrate event capturing.
  • Renders a button, label, and header within the App component.
  • Exports App as the default component.

When Should You Use onClickCapture?

The onClickCapture event is useful when you need to intercept events before they reach their target element. Some common scenarios where it might be helpful include:

  • Logging: You can use onClickCapture to log event details before any other handler processes the event.
  • Validation: If you need to validate or check conditions before allowing an event to continue, onClickCapture allows you to stop event propagation early.
  • Intercepting events: Intercept events for tasks like analytics or tracking clicks without interfering with the standard flow of the event.

Preventing Default Behavior Using onClickCapture

Just like onClick, you can use event.preventDefault() and event.stopPropagation() in the onClickCapture event handler to stop the event from propagating further.

JavaScript
import React from "react";  class PreventDefaultComponent extends React.Component {     handleClickCapture = (event) => {         event.preventDefault();          event.stopPropagation();         console.log("Default behavior prevented during the capture phase");     };      handleClick = () => {         console.log("Event triggered during the bubble phase");     };      render() {         return (             <div onClickCapture={this.handleClickCapture} style={{ padding: "20px" }}>                 <form onSubmit={(e) => e.preventDefault()}>                     <button                         type="submit"                         onClick={this.handleClick}                         style={{ padding: "10px", backgroundColor: "lightblue" }}                     >                         Submit                     </button>                 </form>             </div>         );     } }  export default PreventDefaultComponent; 

Output

onClick

Preventing Default Behavior

In this example

  • onClickCapture handles events during the capture phase (before they reach the target element).
  • handleClickCapture prevents the default behavior (form submission) and stops propagation using event.preventDefault() and event.stopPropagation().
  • handleClick is triggered in the bubble phase, but the event doesn’t reach it because of the capture phase interception.
  • The button click does not submit the form, and the message “Default behavior prevented during the capture phase” is logged.

onClickCapture and Event Propagation

Event propagation in the DOM consists of two phases: the capture phase and the bubble phase. By default, events bubble up from the target element, but with onClickCapture, you can intervene during the capture phase, before the event reaches the target.

Breakdown of Event Propagation with onClickCapture:

  • Click Event is Triggered: The user clicks on an element (e.g., a button).
  • Capture Phase Starts: The event travels down from the root element to the target element (the button).
  • onClickCapture Fires: If there is an onClickCapture handler on any parent element, it fires during the capture phase.
  • Bubble Phase Starts: After the event reaches the target, the bubble phase starts, and the event bubbles up.
  • onClick Fires: The onClick handler on the target element is triggered during the bubble phase.
JavaScript
import React from "react";  class EventPropagation extends React.Component {      handleCapture = (event) => {         console.log("Captured at parent during the capture phase");     };       handleParentClick = (event) => {         console.log("Clicked on parent (Bubble phase)");     };         handleChildClick = (event) => {         console.log("Clicked on child (Bubble phase)");     };      render() {         return (             <div                 onClickCapture={this.handleCapture}                  onClick={this.handleParentClick}                  style={{ padding: "20px", border: "1px solid black" }}             >                 <div                     onClick={this.handleChildClick}                      style={{ padding: "10px", backgroundColor: "lightblue" }}                 >                     Click Me (Child)                 </div>             </div>         );     } }  export default EventPropagation; 

Output

click-me-child

onClickCapture and Event Propagation


In this code

  • handleCapture: This function is triggered during the capture phase on the parent div (using onClickCapture). It fires before the event reaches the target (child element).
  • handleParentClick: This function handles the event during the bubble phase on the parent div (using onClick). It fires after the event reaches the target and bubbles back up to the parent.
  • handleChildClick: This function is triggered during the bubble phase on the child div (using onClick). It fires after the event reaches the child element and begins bubbling up to the parent.

Difference Between onClick and onClickCapture

Featureonclick (Bubble Phase)onClickCapture (Capture Phase)
Trigger TimingFires after the event reaches the target and starts bubbling up.Fires before the event reaches the target while traveling down.
Propagation PhaseBubble phaseCapture phase
Common Use CasesRegular event handlingIntercepting, validation, or logging

Conclusion

The onClickCapture event in React is a powerful feature that allows you to handle click events during the capture phase of event propagation. It is useful for intercepting and managing events before they reach their target elements. This event is often combined with onClick to handle events both in the capture phase and the bubbling phase.



Next Article
What is onPasteCapture Event in ReactJS ?
author
aniluom
Improve
Article Tags :
  • ReactJS
  • Web Technologies
  • React Events
  • React-Questions

Similar Reads

  • What is onDoubleClickCapture Event in ReactJS ?
    React onDoubleClickCapture is an event handler that gets triggered whenever we double click the element. like ondblclick, but the difference is that onDoubleClickCapture acts in the capture phase whereas onDoubleClick acts in the bubbling phase i.e. phases of an event. Prerequisite: Introduction and
    2 min read
  • What is onCutCapture Event in ReactJS ?
    React onCutCapture is an event handler that gets triggered whenever we cut a text. like oncut, but the difference is that onCutCapture acts in the capture phase whereas onCut acts in the bubbling phase i.e. phases of an event. Prerequisites:NodeJS or NPMReactJSSyntax: <input onCutCapture={functio
    1 min read
  • What is onCopyCapture Event in ReactJS ?
    React onCopyCapture is an event handler that gets triggered whenever we copy the text on the webpage. like onCopy, but the difference is that onCopyCapture acts in the capture phase whereas onBlur acts in the bubbling phase i.e. phases of an event. Prerequisites:NodeJS or NPMReact JSPhases of JavaSc
    1 min read
  • What is onChangeCapture Event in ReactJS ?
    ReactJS, a popular JavaScript library for building user interfaces, provides a wide range of events that developers can utilize to create interactive and dynamic applications. One such event is the onChangeCapture event, which is specifically designed for handling changes to form inputs and capturin
    2 min read
  • What is onPasteCapture Event in ReactJS ?
    React onPasteCapture is an event handler that gets triggered whenever we paste some text in an input field. like onpaste, but the difference is that onPasteCapture acts in the capture phase whereas onPaste acts in the bubbling phase i.e. phases of an event. Prerequisites:NodeJS or NPMReact JSPhases
    1 min read
  • What is onBlurCapture Event in React JS ?
    React JS is a popular JavaScript library for building user interfaces, and it provides a wide range of event-handling mechanisms to create interactive web applications. One such event in React JS is the onBlurCapture event. We will explore what the onBlurCapture event is, how it works, and how it ca
    2 min read
  • What is onMouseDownCapture Event in ReactJS?
    React onMouseDownCapture is an event handler that gets triggered whenever we press the mouse button. like onMouseDown, but the difference is that onMouseDownCapture acts in the capture phase whereas onMouseDown acts in the bubbling phase i.e. phases of an event. Prerequisites:NodeJS or NPMReact JSPh
    1 min read
  • What is onMouseMoveCapture Event in ReactJS ?
    The onMouseMoveCapture in React serves as an event handler activated when the mouse moves over an element. Unlike onMouseMove, it operates during the capture phase, contrasting with onMouseMove, which functions during the bubbling phase of an event. Prerequisite:React JSPhases of JavaScript EventApp
    2 min read
  • What is onBeforeInputCapture Event in ReactJS ?
    onBeforeInputCapture Event in ReactJS is an event handler that gets triggered when we make any modifications to the input file, like onBeforeInput, but the difference is that onBeforeInputCapture acts in the capture phase whereas onBeforeInput acts in the bubbling phase i,e. phases of an event. Prer
    2 min read
  • What is the purpose of the onClick event in React?
    The onClick event plays an important role for enhancing user interactions with components in react applications which allows developers/programmers to call a function or method that gets executed when a particular element is clicked. Whether triggering a state change, navigating to another page, or
    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