问题:想为表格实现一列编辑按钮,并且点击按钮会触发相应的事件
先写了以下的代码,发现this为undefined,所以无法调用this,也就没法去触发组件下的方法
{
title: '操作',
dataIndex: '',
key: 'x',
render: (record, row) =>
(<span>
<a href="#" onClick={() => this.handleEdit(true)}>编辑</a>
</span>)
}]
解决办法,通过props传值的方法,由父组件将方法传递给子组件
<Table dataSource={data}
columns={columns({
handleEdit: this.setModal1Visible
})}
pagination={{total, onChange: this.handleSearch}}>
</Table>
const columns = (props) => {
const { handleEdit } = props;
return [{
title: '操作',
dataIndex: '',
key: 'x',
render: (record, row) =>
(<span>
<a href="#" onClick={() => handleEdit(true)}>编辑</a>
</span>)
}]
};
这样就可以访问到父组件的setModal1Visible方法了