如何在链接中添加onClick事件

时间:2019-10-13 10:45:45

标签: reactjs hyperlink

我在ReactJs中的Link代码下面,试图为其添加未触发的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>
    )
   }
   }

为什么我的点击事件没有被触发?组件和容器之间是否缺少某些连接?怎么了?

1 个答案:

答案 0 :(得分:1)

您像这样导出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>
   )
}