Reputation: 21
How to trigger click event of another button while button onclick event in reactjs?
I want to render html and download as excel file while clicking on button event. I used react-workbook for export the excel file.
Upvotes: 0
Views: 92
Reputation: 1831
You can handle onClick event in whatever button and fire your desired method:
import myCsv from './route-to-file.csv';
class MyReactClass extends React.Component {
handleDownload = () => {
const a = document.createElement('a');
a.download = 'fileName.csv';
a.href = `data:text/csv;charset=UTF-8,/${encodeURIComponent(myImage)}`;
a.click();
}
render() {
return <button onClick={this.handleDownload}>Download file</button>
}
}
PD. Updated to handle csv file type.
Upvotes: 1