![](/img/trans.png)
[英]ReactJS how to call a component function from another function on the same file?
[英]How to call function in ReactJS in a callback within the same component?
我在组件中有一个函数,同时我正在使用DataTables按钮触发显示模式。 这是代码段:
constructor(props) {
super(props);
this.state = {
error: null,
isLoaded: false,
table_type: 'default',
date_time : null,
data: [],
header : null
};
this.toogleBtnTbl = this.props.toogleBtnTbl.bind(this);
this.showModal = this.props.showModal.bind(this);
}
我无法在数据表的按钮内调用函数this.showModal
,因为在这种情况下, this
引用了数据表。 我的问题是如何在action
属性中调用this.showModal
?
buttons: [
{
text: 'View in Graph',
action: function ( e, dt, node, config ) {
this.showModal();//not working undefined
}
},
您必须更改为箭头功能
action: ( e, dt, node, config ) => {
this.showModal();
}
从未使用过DataTables,但我认为您只需要一个匿名的立即执行函数即可捕获“ this”并将其存储在闭包中:
buttons: [
{
text: 'View in Graph',
action: (function(component){
return function ( e, dt, node, config ) {
component.showModal();
};
})(this);
},
action被设置为一个函数,该函数立即称为在其自己的小词法环境中存储“ this”,在该环境中,返回的函数将始终可以访问它。 让我知道这个是否奏效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.