我使用antd作为我的UI框架。我试图从类外部调用一个在类中声明的函数,我该怎么做?ReactJS - 从外部调用函数
const columns = [
{
title: 'Action',
dataIndex: 'action',
key: 'action',
render: (text, record, index) => {
if (record.status === 'error') {
return <a className='error-text' href="#">{text} »</a>
} else if (record.status === 'draft') {
return <a href="#" onClick={(e) => { e.stopPropagation(); showModal(record.id); } }><Icon type="delete" /> Delete</a>
} else if (record.status === 'progress') {
return 'N/A'
} else {
return <a href="#">{text} »</a>
}
}
]
class OnBoard extends Component {
constructor(props) {
super(props);
this.state = {
modalVisible: false
}
}
showModal = (id) => {
console.log('i am clicking id: '+id);
}
render() {
return (
<Base page={this.props.location.pathname} title="TOUR LIST">
<div id="dashboard">
<Table
style={{clear: 'both'}}
dataSource={this.state.data}
columns={columns}
loading={this.state.loading}
pagination={this.state.pagination}
onChange={this.handleTableChange}
/>
<Modal
title="Delete Tour"
visible={this.state.modalVisible}
onOk={this.handleOk}
okText="Confirm"
cancelText="Cancel"
onCancel={this.handleCancel}
>
<p>Please confirm you would like to delete the Tour: blablah.xls</p>
</Modal>
</div>
</Base>
);
}
错误: '的ShowModal' 没有定义没有民主基金
我试图将其更改为:
showModal(id){
console.log('i am clicking id: '+id);
}
但我仍然得到了同样的错误。
我已经添加了渲染功能以显示更广泛的上下文。
你不能。你当前的设置没有对类实例的引用,如果你在这个渲染函数中有一个这样的上下文,你应该检查它是否是你的OnBoard类,那么你应该可以用' this.showModal'。任何你可以创建一个MCVE – Icepickle
的机会你可以在你尝试调用'showModal'的地方添加代码吗? –
@Ippickle我添加了更多上下文的渲染函数。 –