react-modal - 我想在反应模式中打开点击的图像
问题描述
我读了很多类似的问题,但没有奏效。我想将在图库中单击的对象打开到模态中。我的问题是当用户点击图片打开数组中最后一个对象的模态时。我不知道如何只制作选择打开的模式。
class SingleGallery extends Component{
constructor () {
super();
this.state = {
showModal: false
};
this.handleOpenModal = this.handleOpenModal.bind(this);
this.handleCloseModal = this.handleCloseModal.bind(this);
}
handleOpenModal () {
this.setState({ showModal: true });
}
handleCloseModal () {
this.setState({ showModal: false });
}
render(){
let singleGalleryObj = [
{image: imageGallery1},
{image: imageGallery2},
{image: imageGallery3},
{image: imageGallery4},
{image: imageGallery1},
{image: imageGallery2}
]
return(
<div class="singleGallery">
<div class="SGContent">
<div class="title">Gallery 1</div>
<div class="images">
{singleGalleryObj.map((para) => {
return (
<div>
<div class="galleryBox" onClick={this.handleOpenModal}>
<div class="image">
<img src={para.image} />
</div>
</div>
<Modal
isOpen={this.state.showModal}
contentLabel="Minimal Modal Example"
>
<button onClick={this.handleCloseModal}>Close Modal</button>
<img src={para.image}/>
</Modal>
</div>
)
})}
</div>
</div>
</div>
)
}
}
解决方案
我认为问题出在控制器上。您只有 1 个控制器来显示或隐藏模式。您的渲染功能只能有 1 个模态,例如
<div class="singleGallery">
<div class="SGContent">
<div class="title">Gallery 1</div>
<div class="images">
{singleGalleryObj.map((para) => {
return (
<div>
<div class="galleryBox" onClick={() => this.handleOpenModal(para)}>
<div class="image">
<img src={para.image} />
</div>
</div>
</div>
)
})}
<Modal
isOpen={this.state.showModal}
contentLabel="Minimal Modal Example"
>
<button onClick={this.handleCloseModal}>Close Modal</button>
{this.state.selectedImage ? <img src={selectedImage.image}/> : null}
</Modal>
</div>
</div>
</div>
你的开放模式处理程序可能就像
handleOpenModal (selectedImage) {
this.setState({ showModal: true, selectedImage });
}
让我知道它是否有效。
推荐阅读
- reactjs - React 中的嵌套 ApolloProviders
- amazon-ecs - 获取 AWS Batch 集群名称 CDK
- xamarin.ios - UICollectionView Xamarin.ios 中的 UILabel 重叠
- python - 带有熊猫条形图的 axvline 使用句点
- javascript - 如何检查数组是否包含 Javascript 中 JSON 数据的值?
- node.js - 如何在nodejs中使函数同步
- reactjs - 当我在 redux saga 中提交表单数据时有时会返回成功有时不会
- flutter - Flutter:在小部件测试中未调用 Mapbox onMapCreated
- c# - CosmosDB 文档客户端 - 如何为查询隐式添加类型?
- little-man-computer - 对输入数字求和的程序不起作用