【问题标题】:Button onClick event inside react datatable doesn't work反应数据表中的按钮 onClick 事件不起作用
【发布时间】:2020-03-18 05:24:18
【问题描述】:

我编写了一个渲染函数,并在数据表的每一行中提供了用于编辑和删除的按钮。 onclick 事件正在使用 react redux 操作方法。但是当我单击编辑/删除按钮时,我得到 'Cannot read property'props' of undefined' 错误

import { DataTable } from 'react-data-components';
    <DataTable
      keys="title"
       columns={[
       { title: 'Report Name', prop: 'title'},
       { title: 'Author', prop: 'username'},
       { title: 'date Created', prop: 'datecreated'},
       { title: 'Date Modified', prop: 'datelastmodified'},
       {title:'Actions', render:function(val, row) {
          return React.createElement( "div", null,
           React.createElement( "button", {className:"btn", onClick:(e)=>this.props.EditReport(row.reportid)}, 'Edit'),
           React.createElement( "button", {className:"btn", onClick:(e)=>this.props.DeleteReport(row.reportid)}, 'Delete')
       )
     }
     }
  ]}
  initialData={this.props.savedReportList.data}
  initialPageLength={5}
 />

const mapDispatchToProps = (dispatch) => {
  return {
      EditReport: (reportID) => dispatch(EditReport(reportID)),
      DeleteReport: (reportID) => dispatch(DeleteReport(reportID))
  }
}

【问题讨论】:

  • 这是您的完整代码吗?你是用类基还是函数基组件?
  • 另外你用过 react-redux 的 connect 函数吗?
  • 是的,我已经导入了 connect 以及 redux 操作方法。从'react-redux'导入{连接};从 './Actions' 导入 {EditReport,DeleteReport};

标签: reactjs react-redux


【解决方案1】:

发生这种情况是因为您使用了超出范围的this。你想达到什么目的?我们可以从那里拿走它。

【讨论】:

  • 如果我从上面的代码中删除 Action 列,即使 initialData 使用“this”范围内的数据,dataTable 也会显示数据。那么为什么只有在渲染函数中'this'超出范围
猜你喜欢
  • 1970-01-01
  • 2022-09-24
  • 1970-01-01
  • 2017-07-18
  • 1970-01-01
  • 2014-10-30
  • 2014-12-17
  • 1970-01-01
  • 2021-11-01
相关资源
最近更新 更多