3

I have this component code:

const myCardItem = ({ item }) => {

    function clickItem(){
        alert('Item clicked');
    }

    function clickMe(){
        alert('Button clicked');
    }

    return (
        <div className="myCardItem" onClick={clickItem}>
            <i className="fa fa-clock-o"/>&nbsp;{item.planDate}
            <br />
            <i className="fa fa-location-arrow"/>&nbsp;{item.destination}
            <br />
            <Button bsStyle="primary" onClick={clickMe}>Click Me</Button>
        </div>
    );
}

If I click the button I get first:

enter image description here

Followed by:

enter image description here

Which makes sense, because the button is a child under the myCardItem. But that's not what I want. Clicking the myCardItem will load a new view, while clicking the button would open a modal dialog. Having both go off would break the page.

How can I prevent the parent click being called?

It's not as easy as doing a return false in clickMe() ...

Spikee
  • 3,967
  • 7
  • 35
  • 68

1 Answers1

6

Use event.stopPropagation() or event.stopImmediatePropagation() these stop event bubbling and end bubble of events where it is called.

Deckerz
  • 2,606
  • 14
  • 33
simbathesailor
  • 3,681
  • 2
  • 19
  • 30