Ad
How To Add OnClick Event In Link
I have below Link
code in ReactJs for which trying to add onClick
event which is not getting fired.
Functional stateless Component
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>
);
}
and here is my 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>
)
}
}
Why my click event is not getting fired? Is some connectivity missing between component and Container? What's wrong?
Ad
Answer
You export ResultChip
like this:
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>
);
}
Call ResultChip
in your ContainerComponent
:
import ResultChip from '...';
downloadTestResults = (e) => {
e.preventDefault();
//Perform some action here
}
render() {
return (
<div>
<ResultChip
downloadTestResults={this.downloadTestResults}
chipName={...}
/>
</div>
)
}
Ad
source: stackoverflow.com
Related Questions
- → Import statement and Babel
- → should I choose reactjs+f7 or f7+vue.js?
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → .tsx webpack compile fails: Unexpected token <
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → React Native with visual studio 2015 IDE
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
- → How do I determine if a new ReactJS session and/or Browser session has started?
- → Alt @decorators in React-Native
- → How to dynamically add class to parent div of focused input field?
Ad