javascript - 如何在点击时安装组件?

标签 javascript reactjs csv

我正在使用这个react-csv ,并且仅在安装时才触发下载。问题是我有一个普通的 Material 按钮,我需要在单击该按钮时触发该按钮:

<MButton id="csv-download" onClick={<CSVDownload data={this.state.rowData} filename={"relatorio.csv"} target="csv-download"/>} style={{
    ...buttons.action,
    ...buttons.topRight
}}>Exportar</MButton>

onClick 内部的组件听起来很荒谬,但我把它放在那里是为了说明我的观点。我该怎么做?

最佳答案

我没有测试过这个,但我过去做过类似的事情:

downloadCSV() {
  this.setState({
    downloadCSV: true
  })
}

renderCSVDownload() {
  if(this.state.downloadCSV) {
    return (
      <CSVDownload 
        data={this.state.rowData} 
        filename={"relatorio.csv"} 
        target="csv-download"/>
    )
  }
}

render() {
  return (
    <div>
      <MButton id="csv-download" onClick={this.downloadCSV} style={{
          ...buttons.action,
          ...buttons.topRight
      }}>Exportar</MButton>
      {this.renderCSVDownload()}
    </div>
  )
}

虽然 CSVLink 已经完成了您正在尝试的操作,但它不是 Material 按钮 (MButton):

<CSVLink
  data={this.state.rowData} 
  filename={"relatorio.csv"} 
  target="csv-download"
>Exportar</CSVLink>

关于javascript - 如何在点击时安装组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56342279/

相关文章:

javascript - 如何在 Node 中的 process.exit 之后启动另一个进程

reactjs - 使用react-三纤在 gatsby 站点中加载 gltf

javascript - 无法通过 auth.onAuthStateChanged 中的 currentUser 访问 displayName - Firebase

mysql - 如何控制表单中的字符编码是否正确存储了法语口音

javascript - 运行一次 JS 函数

javascript - 无法在js中将数据推送到空数组中

javascript - 使用 src 引用 Javascript 或直接将其注入(inject) HEAD 在性能和内存占用方面的差异

reactjs - 保持 Material UI 选项卡和 React Router 同步

python - 如何在Python中将CSV文件转换为字典列表

python - 将 float 列表写入 csv 文件