【问题标题】:(react,antd,card) How can I block the onclick function from action buttons with antd card component?(react,antd,card) 如何使用 antd card 组件阻止操作按钮的 onclick 功能?
【发布时间】:2020-09-26 17:18:28
【问题描述】:

点击antd卡有问题。

Antd card layout 这是我的antd卡布局。 我通过 onclick 方法使用 Card 组件 控制台日志。

但是当我点击1,2,3,4区域(antd卡片布局链接)时,它们都控制台“点击卡片”消息。我认为这是正确的操作,因为它们位于 Card 组件中,但我想使用以下操作按钮阻止 onclick 操作。最后,只有当我单击 1,2 区域时,它才应该控制台。

下面是我的卡片组件代码。

<div> 
    <Card
        onClick={()=>console.log("clicked card")}
        hoverable
        cover={
            bucket.imgSrc === "" ? 
            <img 
                alt="random img"
                src="https://source.unsplash.com/random"
            />
            :
            <img
                alt="bucket img"
                src={bucket.imgSrc}
            />
        }
        actions={[
            <SearchOutlined key="edit" onClick={this.popupModal}/>,
            this.state.like ? 
                <Button icon={<HeartFilled />} type='link' key='like' onClick={this.handleDislike}> {this.state.likeCount}</Button>
                :
                <Button icon={<HeartOutlined />} type='link' key='dislike' onClick={this.handleLike}> {this.state.likeCount}</Button>
        ]}
        >

        <Card.Meta
            avatar={<UserAvatar photoURL={bucket.userPhotoURL} />}
            title={bucket.title}
            description={bucket.description}
        />
    </Card>
</div>

【问题讨论】:

    标签: javascript reactjs antd


    【解决方案1】:

    在其他事件上定义e.stopPropagation(),例如:

    actions={[
       <SearchOutlined key="edit" onClick={event => { event.stopPropagation(); this.popupModal}} />
       ...
    ]}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-03
      • 1970-01-01
      • 2022-01-07
      • 2021-07-01
      • 2020-04-15
      • 2020-09-27
      • 1970-01-01
      • 2021-03-08
      相关资源
      最近更新 更多