reactjs - 如何在链接中添加 onClick 事件
问题描述
我Link
在 ReactJs 中有以下代码,试图添加onClick
没有被触发的事件。
功能性无状态组件
export default function ResultChip({chipName, downloadTestResults}) {
return (
<Card className={classes.card}>
<CardContent>
{
chipName == "Download results" &&
<Typography style ={{marginTop: 15, fontWeight: 3}}>
<Link style={{ fontWeight: 2, color: '#087063', letterSpacing: '0.28' }} onClick={downloadTestResults}> {botTestAPIResult.uploadedFilename} </Link>
</Typography>
}
</CardContent>
</Card>
);
}
这是我的 ContainerComponent
import React, { Component } from 'react';
class ResultChip extends Component {
constructor(props) {
super(props);
this.state= {
}
this.downloadTestResults = this.downloadTestResults.bind(this);
}
downloadTestResults = (e) => {
e.preventDefault();
//Perform some action here
}
render() {
return (
<div>
</div>
)
}
}
为什么我的点击事件没有被触发?组件和容器之间是否缺少某些连接?怎么了?
解决方案
你像这样导出ResultChip
:
export default function ResultChip({chipName, downloadTestResults}) {
return (
<Card className={classes.card}>
<CardContent>
{
chipName == "Download results" &&
<Typography style ={{marginTop: 15, fontWeight: 3}}>
<Link onClick={downloadTestResults}> {botTestAPIResult.uploadedFilename}</Link>
</Typography>
}
</CardContent>
</Card>
);
}
致电ResultChip
您的ContainerComponent
:
import ResultChip from '...';
downloadTestResults = (e) => {
e.preventDefault();
//Perform some action here
}
render() {
return (
<div>
<ResultChip
downloadTestResults={this.downloadTestResults}
chipName={...}
/>
</div>
)
}
推荐阅读
- python - 将 python dicts 的 pyspark pipelineRDD 解压缩到 pyspark Dataframe
- websocket - 服务器到服务器 websocket
- oracle - 用于计算嵌套表内属性的 Oracle 触发器
- python - 为什么 tkinter 绑定事件只触发一次?
- discord.py - 如何在 discord.py 中获得命令的主要“功能”?
- javascript - 如何使用 chartjs 和 Angular 制作图表?
- csv - 如何将 Yahoo Finance 中的历史数据导入 Google 表格并使其自动更新?
- php - 如何用循环填充多维数组
- javascript - 如何在发出事件 Socket.io 后监听事件
- mysql - 如何从mysql中的group by和groupconcat其他字段中获取最大值?