What is onClickCapture Event in ReactJS?
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.
//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.
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

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.
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

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
Feature | onclick (Bubble Phase) | onClickCapture (Capture Phase) |
---|---|---|
Trigger Timing | Fires after the event reaches the target and starts bubbling up. | Fires before the event reaches the target while traveling down. |
Propagation Phase | Bubble phase | Capture phase |
Common Use Cases | Regular event handling | Intercepting, 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.