javascript - 在 onClick 中 react onClick

标签 javascript reactjs dom-events

我正在尝试制作一个图像,当您单击它时,它会将您带到显示页面,但在该图像内部,我还需要一些按钮来制作其他东西:

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

我的问题是当我单击按钮时,它也需要 div onClick。

最佳答案

更改您的 Button onClick 以停止事件传播到父级

<Button
     className="goto"
     onClick={(e) => {
             e.stopPropagation(); 
             this.handleRefs(dwelling.address, index)
     }}>
     <FontAwesome name="map-marker" size="25"/>
</Button>{' '}

关于javascript - 在 onClick 中 react onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50512603/

相关文章:

javascript - HTML 使常规按钮像一组单选按钮一样工作

javascript - 在 javascript 中创建玩具数组

javascript - React - 使样式对象全局可访问

javascript - 如何使用输入类型文件使用 JavaScript 将文本从文件加载到文本区域?

javascript - 如何使用 JavaScript 检测页面 404 错误?

javascript - 使用 eval 将变量分配给方法。我还应该怎么做呢?

javascript - iOS Swift 如何将 JSON 字符串和数组或字典从 UIWebView 传递给 Javascript?

reactjs - 在 React.js 中通过鼠标拖动绘制矩形的最佳方法?

javascript - 如何在 React 函数组件中调用 API?

javascript - 动态声明和使用 javascript 值