【发布时间】:2018-09-09 01:07:21
【问题描述】:
我正在使用 react-data-components 来呈现表格。在表格内,我呈现了一个删除按钮。但是,在 render 方法中,它无法访问“this”关键字,因此无法访问按钮单击时的任何处理函数。我知道如何在父组件和子组件之间进行通信,但这似乎不属于该类别。代码编译但在运行时失败。错误是:未捕获的类型错误:无法读取未定义的属性“handleClick” 任何帮助表示赞赏。
这是我的代码:
接口 MyComponentProps 扩展 RouteComponentProps {
appState: AppState;
}
@inject('appState')
class MyComponent 扩展 React.Component {
constructor(props: MyComponentProps, context: object) {
super(props, context);
}
renderLink(val: any, row: any) {
console.log(this); //'this' is undefined
return (
<button onClick={this.handleClick}>Delete</button>
);
}
handleClick() {
console.log('click');
// access appState here
}
render() {
var columns =
[
{ title: '', prop: 'Id', render: this.renderLink },
{ title: 'Name', prop: 'Name' }
];
let data = [];
// code to populate data
return (<DataTable keys="fileId" columns={columns} initialData={data} />);
}
}
导出默认的MyComponent;
【问题讨论】:
标签: reactjs