3
我正在嘗試使用map渲染多個react-bootsrap模態,但我無法這樣做。用我現在的代碼點擊'View Details'按鈕,可以同時激活所有的模態而不是打開相關的模態。下面是我的代碼片段相關的模式:在react-bootstrap中正確渲染多個模態
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>
)
})
感謝您的解決方案。太棒了。你能解釋一下代碼嗎?我是一名初學者。 –
當然,我編輯了一些更多細節的答案,希望這有助於! –