Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React onClick inside onClick

I'm trying to do an image that when you click on it, takes you to a show page, but also inside that image I need to have some buttons that make other things:

<div
className="prop-detail"
key={dwelling._id}
onClick={() => this.props.history.push(`/propiedades/${dwelling._id}`)}
>

<img
src={dwelling.images[0] !== undefined ? dwelling.images[0].secure_url: 'https://res.cloudinary.com/sioc/image/upload/v1525712940/epnvioppkpvwye1qs66z.jpg'}
alt=""
/>

<div className="prop-text">
{dwelling.price ? <span>{dwelling.price} {dwelling.currency}</span> : <span>Consulte</span>}
<small> {dwelling.spaces.rooms}h - {dwelling.spaces.bathRoom}b</small>
<p>Calle {dwelling.address.streetName} {dwelling.address.streetNumber}, {dwelling.address.city}, {dwelling.address.state}</p>
</div>

<div className="prop-detail-btns">
<Button
className="goto"
onClick={() => this.handleRefs(dwelling.address, index)}>
<FontAwesome name="map-marker" size="25"/>
</Button>{' '}
<Button className="like">
<FontAwesome name="heart" size="25"/>
</Button>
</div>
</div>

My problem is when I click on the button it also takes the div onClick.

like image 453
Nicolas Albarracin Avatar asked May 24 '18 15:05

Nicolas Albarracin


1 Answers

Change your Button onClick to stop event propagation to parent

<Button
     className="goto"
     onClick={(e) => {
             e.stopPropagation(); 
             this.handleRefs(dwelling.address, index)
     }}>
     <FontAwesome name="map-marker" size="25"/>
</Button>{' '}
like image 99
Bobz Avatar answered Nov 13 '22 05:11

Bobz