在react功能组件中使用相同代码时缺少按钮



在类组件(typescript文件(中,我以以下方式定义了actionTemplate

//Inside constructor: 
constructor(props: Props) {
super(props);
this.actionTemplate = this.actionTemplate.bind(this);

this.state = {
//state variables
};
}
actionTemplate = (rowData: any) => (
<div style={{textAlign: 'center', width: '6em'}}>
<span>
<Button type='button' icon="pi pi-pencil" style={{marginRight: '5px'}} onClick={(e) => this.handleClick(rowData, e)} tooltip='Edit'/>
<Button icon="pi pi-trash" style={{marginRight: '5px'}} tooltip='Delete' />
{
rowData.fullPathName !== null &&
<Button icon="pi pi-download" tooltip='Download' onClick={(e) => this.handleDownloadClick(rowData, e)} />
}
</span>
</div>
);  
const GridView = labelValue => ((this.state.assocVisible || this.state.assetFormVisible)? null: (
<div>
<DataTable
//some datable properties
>
<Column key='actionButton' field='actionButton' header='Action' body={this.actionTemplate} style={{maxHeight: '30px !important', whiteSpace: 'nowrap', overflow: 'hidden'}}/>
{dynamicColumns}
</DataTable>

</div>
))

然而,我在一个没有类的功能组件中使用了同样的东西,我做了以下操作(正如本线程中建议的那样——使用箭头函数(:

actionTemplate = (rowData) => (
<div style={{textAlign: 'center', width: '6em'}}>
<span>
<Button type='button' icon="pi pi-pencil" style={{marginRight: '5px'}} onClick={(e) => handleClick(rowData, e)} tooltip='Edit'/>
<Button icon="pi pi-trash" style={{marginRight: '5px'}} tooltip='Delete' />
{
rowData.fullPathName !== null &&
<Button icon="pi pi-download" tooltip='Download' onClick={(e) => handleDownloadClick(rowData, e)} />
}
</span>
</div>
);
const GridView = labelValue => ((assocVisible[0] || assetFormVisible[0])? null: (
<div>
<DataTable
//some datable properties 
>
<Column key='actionButton' field='actionButton' header='Action' body={actionTemplate} style={{maxHeight: '30px !important', whiteSpace: 'nowrap', overflow: 'hidden'}}/>
{dynamicColumns}
</DataTable>
{/* {assocButtonView(labelValue)} */}
</div>
))

它不断抛出以下错误:

Uncaught ReferenceError: assignment to undeclared variable actionTemplate

当我在actionTemplate前面添加var时,它没有抛出任何错误,但我在Action列中没有看到任何按钮。我错过了什么?

在函数组件中,您没有声明另一个函数,您必须将该函数分配给一个变量。

所以这是正确的语法:

const actionTemplate = (rowData) => (
<div style={{textAlign: 'center', width: '6em'}}>
<span>
<Button type='button' icon="pi pi-pencil" style={{marginRight: '5px'}} onClick={(e) => handleClick(rowData, e)} tooltip='Edit'/>
<Button icon="pi pi-trash" style={{marginRight: '5px'}} tooltip='Delete' />
{
rowData.fullPathName !== null &&
<Button icon="pi pi-download" tooltip='Download' onClick={(e) => handleDownloadClick(rowData, e)} />
}
</span>
</div>
);
const GridView = labelValue => ((assocVisible[0] || assetFormVisible[0])? null: (
<div>
<DataTable
//some datable properties 
>
<Column key='actionButton' field='actionButton' header='Action' body={actionTemplate} style={{maxHeight: '30px !important', whiteSpace: 'nowrap', overflow: 'hidden'}}/>
{dynamicColumns}
</DataTable>
{/* {assocButtonView(labelValue)} */}
</div>
))

相关内容

  • 没有找到相关文章