What is onClickCapture Event in ReactJS? (original) (raw)

Last Updated : 19 Mar, 2025

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

**Syntax

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 (

Hey Geek!

Please click on the button click Me!
); }

export default App;

`

**Output

**In this example

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:

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

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

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