在 react-bootstrap 中使用地图正确渲染多模态
Render Multiple Modals correctly with map in react-bootstrap
我正在尝试使用地图渲染多个 react-bootsrap 模态,但我无法这样做。使用我当前的代码,单击 'View Details' 按钮会同时激活所有模态,而不是打开相关模态。这是我与模式相关的代码片段:
render() {
const { accounts } = this.props;
const displayAccounts = Object.keys(accounts).filter(key => {
return accounts[key].info.type === 'student'
}).map(key => {
return (
<tr key={key}>
<th>{accounts[key].info.name}</th>
<td>{accounts[key].info.email}</td>
<td><Button bsStyle='danger' onClick={() => this.props.removeAccount(key)}>Remove Account</Button></td>
<td>
<ButtonToolbar>
<Button id={key} bsStyle="primary" onClick={this.showModal}>View Details</Button>
<Modal
id={key}
show={this.state.show}
onHide={this.hideModal}
>
<Modal.Header closeButton>
<Modal.Title>Student Details</Modal.Title>
</Modal.Header>
<Modal.Body>
<Table responsive striped hover>
<thead>
<tr>
<th>Title</th>
<th>Details</th>
</tr>
</thead>
<tbody>
<tr>
<th>Name</th>
<td>{accounts[key].userDetails.name}</td>
</tr>
<tr>
<th>Education</th>
<td>{accounts[key].userDetails.education}</td>
</tr>
<tr>
<th>GPA</th>
<td>{accounts[key].userDetails.gpa}</td>
</tr>
<tr>
<th>Skills</th>
<td>{accounts[key].userDetails.skills}</td>
</tr>
<tr>
<th>Overview</th>
<td>{accounts[key].userDetails.skills}</td>
</tr>
</tbody>
</Table>
</Modal.Body>
<Modal.Footer>
<Button onClick={this.hideModal}>Close</Button>
</Modal.Footer>
</Modal>
</ButtonToolbar>
</td>
</tr>
)
})
尝试进行以下修改...
将以下更改添加到 constructor
中的 this.state
以缓存稍后分配的活动模态索引。
this.state = {
...,
activeModal: null,
...,
}
this.clickHandler = this.clickHandler.bind(this);
this.hideModal = this.hideModal.bind(this);
Add/Change 以下事件处理程序。 clickHandler
接受点击事件以及将用于设置上述 activeModal
状态的索引。当 React 发现状态发生变化时,它将使用新状态调用 render
方法。这就是 React 的 Reactive 本质。
clickHandler(e, index) {
this.setState({ activeModal: index })
}
hideModal() {
this.setState({ activeModal: null })
}
在你的 map
函数中做这样的事情。注意 onClick
处理程序。使用箭头函数捕捉点击事件并调用您的 clickHandler
,这样您还可以传递其他参数(在本例中为 index
)。一旦 activeModal
状态片段被调用,组件将被重新渲染,在对 show
属性进行操作后,适当的 clicked
组件将评估为 true。
buttonArray.map((button, index) => {
<Button id={key} bsStyle="primary" onClick={e => this.clickHandler(e, index)}>View Details</Button>
<Modal
id={key}
show={this.state.activeModal === index}
onHide={this.hideModal}
/>
} )
我正在尝试使用地图渲染多个 react-bootsrap 模态,但我无法这样做。使用我当前的代码,单击 'View Details' 按钮会同时激活所有模态,而不是打开相关模态。这是我与模式相关的代码片段:
render() {
const { accounts } = this.props;
const displayAccounts = Object.keys(accounts).filter(key => {
return accounts[key].info.type === 'student'
}).map(key => {
return (
<tr key={key}>
<th>{accounts[key].info.name}</th>
<td>{accounts[key].info.email}</td>
<td><Button bsStyle='danger' onClick={() => this.props.removeAccount(key)}>Remove Account</Button></td>
<td>
<ButtonToolbar>
<Button id={key} bsStyle="primary" onClick={this.showModal}>View Details</Button>
<Modal
id={key}
show={this.state.show}
onHide={this.hideModal}
>
<Modal.Header closeButton>
<Modal.Title>Student Details</Modal.Title>
</Modal.Header>
<Modal.Body>
<Table responsive striped hover>
<thead>
<tr>
<th>Title</th>
<th>Details</th>
</tr>
</thead>
<tbody>
<tr>
<th>Name</th>
<td>{accounts[key].userDetails.name}</td>
</tr>
<tr>
<th>Education</th>
<td>{accounts[key].userDetails.education}</td>
</tr>
<tr>
<th>GPA</th>
<td>{accounts[key].userDetails.gpa}</td>
</tr>
<tr>
<th>Skills</th>
<td>{accounts[key].userDetails.skills}</td>
</tr>
<tr>
<th>Overview</th>
<td>{accounts[key].userDetails.skills}</td>
</tr>
</tbody>
</Table>
</Modal.Body>
<Modal.Footer>
<Button onClick={this.hideModal}>Close</Button>
</Modal.Footer>
</Modal>
</ButtonToolbar>
</td>
</tr>
)
})
尝试进行以下修改...
将以下更改添加到 constructor
中的 this.state
以缓存稍后分配的活动模态索引。
this.state = {
...,
activeModal: null,
...,
}
this.clickHandler = this.clickHandler.bind(this);
this.hideModal = this.hideModal.bind(this);
Add/Change 以下事件处理程序。 clickHandler
接受点击事件以及将用于设置上述 activeModal
状态的索引。当 React 发现状态发生变化时,它将使用新状态调用 render
方法。这就是 React 的 Reactive 本质。
clickHandler(e, index) {
this.setState({ activeModal: index })
}
hideModal() {
this.setState({ activeModal: null })
}
在你的 map
函数中做这样的事情。注意 onClick
处理程序。使用箭头函数捕捉点击事件并调用您的 clickHandler
,这样您还可以传递其他参数(在本例中为 index
)。一旦 activeModal
状态片段被调用,组件将被重新渲染,在对 show
属性进行操作后,适当的 clicked
组件将评估为 true。
buttonArray.map((button, index) => {
<Button id={key} bsStyle="primary" onClick={e => this.clickHandler(e, index)}>View Details</Button>
<Modal
id={key}
show={this.state.activeModal === index}
onHide={this.hideModal}
/>
} )