reactjs - 如何使卡片组件可点击?
问题描述
我将 Ant Design 用于我的 WebApp。对于Card,有一个 hoverable 道具使卡片接缝可点击,但没有 onClick 道具。如何让它真正可点击?
这是我的代码:
import React, { Component } from 'react';
import { Card, Avatar, Icon, Button, Divider } from 'antd';
import EventDetailsDrawer from '../ui/EventDetailsDrawer';
const { Meta } = Card;
class EventCard extends Component {
render() {
return (
<div onClick={alert("Hello from here")}>
<Card
hoverable
cover={<img alt="example" src="https://assetsnffrgf-a.akamaihd.net/assets/m/1102015169/univ/art/1102015169_univ_lsr_lg.jpg" />}
bodyStyle={{ marginBottom: "-5px" }}
>
<Meta
//avatar={<Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />}
avatar={<Button type="primary" shape="circle-outline">{this.props.owner}</Button>}
title={this.props.title}
description={this.props.descp}
/>
<Divider style={{ marginLeft: "0px" }}></Divider>
<p><Icon type="clock-circle" style={{ fontSize: "15px", color: "#1890FE" }} theme="outlined" /><span style={{ marginLeft: "15px" }} />{this.props.date}</p>
<p><Icon type="environment" style={{ fontSize: "15px", color: "#1890FE" }} theme="outlined" /><span style={{ marginLeft: "15px" }} />{this.props.location}</p>
</Card>
<EventDetailsDrawer></EventDetailsDrawer>
</div>
);
}
}
export default EventCard
我尝试使潜水(围绕卡片)可点击,但代码在应用程序加载后立即运行,因为我将每张卡片打包到一个列表项中。如何使卡片组件可点击?
感谢您的回答 :)
解决方案
请注意,您附加到 div 的onClick
侦听器的是返回的值,alert
而不是实际上应该在单击 div 时运行的函数。
尝试改变这个:
<div onClick={alert("Hello from here")}>
对此:
<div onClick={() => alert("Hello from here")}>
推荐阅读
- python - 将 csv 标头与相应的文件路径组合到新文件中
- php - PHP渲染JSON数据集的堆积面积图
- javascript - Wordpress 前端表单不会提交
- python - 在 python 中读取巨大 MySQL 表的最快方法
- .net - WebApi/.net-core: 无法访问 webapi
- node.js - 可以在构建时覆盖 docker 环境变量吗?
- azure-active-directory - 如何以管理员身份向 Azure Active Directory 中的应用程序组授予权限?
- javascript - three.js 扭曲的 boxhelper 和不准确的对象位置
- jquery - 如何在accounting.js中为小数添加小数点?
- android - React Native 代码中的错误