我有一个React Bootstrap Table
,表的每一页都有 20 条记录。在每一行中,我使用以下行添加了一个按钮
function attachFormatter(cell, row){
return (
<AttachmentManager />
);
}
<TableHeaderColumn key={uuid.v4()}
dataField={column.dataField}
dataFormat={attachFormatter}
hidden={hide}>
{column.label}
</TableHeaderColumn>
然后我在这个页面中有 20 个按钮,每一行都有一个按钮。如果单击其中一个按钮,我打算打开一个模式。但是当我单击一个按钮时,openModal()
按预期运行一次,但render()
我的功能AttachmentManager
将运行 20 次。如何解决这个问题?
export default class AttachmentManager extends React.Component {
constructor (props) {
super(props);
this.state = {showModal: false};
}
openModal() {
alert('test');
}
render () {
return (
<button onClick={this.openModal.bind(this)} className="btn btn-default">Add Projects
<AttachmentModal show={this.state.showModal}/>
</button>
);
}
}
以下是我的模态
import React from 'react';
import SimpleModal from '../common/SimpleModal';
export default class AttachmentModal extends React.Component {
constructor (props) {
super(props);
}
render () {
return (
<SimpleModal showModal={this.props.show}
onToggleModal={this.props.onHide}
title={this.props.title}
onCancelClick={this.props.onHide}
onPrimaryButtonClick={this.props.onPrimaryButtonClick}
cancelText="Cancel"
primaryButtonText={this.props.primaryButtonText}
loading={this.props.loading}
backdrop='static'
bsStyle={this.props.bsStyle}>
{this.props.children}
</SimpleModal>
);
}
}