hirak agile
hirak agile

Reputation: 21

Handle Event Listener

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

Answers (1)

Adolfo Onrubia
Adolfo Onrubia

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

Related Questions