我有react模式的问题。我有一个列表,SingleElement
点击后它应该出现模态窗口,其中包含单击元素的更多详细信息。带有数据的 JSON 存储为状态,从 Modal 内部我找不到获取所需元素的方法。
这是模态内部render()
函数:
<Modal
isOpen={this.state.modalIsOpen}
onAfterOpen={this.afterOpenModal}
onRequestClose={this.closeModal}
chosenBeer={this.state.chosenBeer}
>
<h2> == Dynamically changing title of element == </h2>
<button onClick={this.closeModal}>X</button>
<img src="{ == Dynamically changing image == }" />
</Modal>
<div id="splashElements">
{
this.state.elements &&
this.state.elements.map((item, index) => {
return(<SingleElement key={index} name={item.name} href={item.href} image={item.image_url} tagline={item.tagline} onDelete={id => this.onDelete(index)} openModal={elementId => this.openModal(index)}/>)
})
}
</div>
以下是负责显示/隐藏模式窗口的方法:
openModal = (beerId) => {
this.setState({modalIsOpen: true, chosenBeer: this.state.beers[beerId]});
}
afterOpenModal = () =>{
// references are now sync'd and can be accessed.
}
closeModal = () => {
this.setState({modalIsOpen: false});
}