我正在尝试实现一个灵活的显示图像的lightbox组件。我是using this npm,我从api得到的图片地址。
我应该如何将当前图像索引值传递给组件(单击了哪个图像),以便在模式上显示adject图像。
下面是我从后台得到的JSON
{
"count": 3,
"next": "http://127.0.0.1:8000/api/aws/gallery/all-gallery/?page=2",
"previous": null,
"results": [
{ "id": 127, "gallery_img_url": "https://mysite.amazonaws.com/gallery/test2.jpg" },
{ "id": 126, "gallery_img_url": "https://mysite.amazonaws.com/gallery/CaptureXYZ.PNG" },
{ "id": 125, "gallery_img_url": "https://mysite.amazonaws.com/gallery/Capture2.PNG" }
]
}onClick图像我正在将这个id从结果数组传递给组件,我想这可能是问题所在,有人能建议我如何将准确的索引值(准确地说,是点击的图像)传递给组件吗?
这是我的组件
import React, { Component } from 'react'
import { connect } from 'react-redux';
import { getAllGalleryImages } from '../actions/gallery';
import Lightbox from 'react-image-lightbox';
import 'react-image-lightbox/style.css';
class ViewGalleryImage extends Component {
state = {
photoIndex: 0,
isOpen: false,
}
componentDidMount() {
// this will Fetch data from api
this.props.getAllGalleryImages();
window.scrollTo(0, 0)
}
render() {
const { photoIndex, isOpen } = this.state;
const images = [];
this.props.images && this.props.images.results && this.props.images.results.map(result =>
images.push(result.gallery_img_url),
)
return (
<div>
{this.props.images.count === 0 ? <p><strong>No Images found!</strong></p> :
<React.Fragment>
{this.props.images && this.props.images.results && this.props.images.results.map(result =>
<div className='masonry-item' key={result.id}>
<img src={result.gallery_img_url} onClick={() => this.setState({ isOpen: true, photoIndex: result.id })} alt='myImage' className='dlt_blg_img' />
</div>
)}
</React.Fragment>
}
{
isOpen && (
<Lightbox
mainSrc={images[photoIndex]}
nextSrc={images[(photoIndex + 1) % images.length]}
prevSrc={images[(photoIndex + images.length - 1) % images.length]}
onCloseRequest={() => this.setState({ isOpen: false })}
onMovePrevRequest={() =>
this.setState({
photoIndex: (photoIndex + images.length - 1) % images.length,
})
}
onMoveNextRequest={() =>
this.setState({
photoIndex: (photoIndex + 1) % images.length,
})
}
/>
)
}
</div>
)
}
}
const mapStateToProps = state => ({
isLoading: state.gallery.isLoading,
images: state.gallery
});
export default connect(
mapStateToProps,
{ getAllGalleryImages }
)(ViewGalleryImage);发布于 2019-10-15 16:34:01
我已经找到了一个简单的解决方案。我想这对像我这样的新手有帮助。
下面是更新组件的一部分
<div>
{this.props.images.count === 0 ? <p><strong>No Images found!</strong></p> :
<React.Fragment>
{
images.map((index, key) =>
<div className='masonry-item' key={key}>
<img src={index} alt='gallery' onClick={() => this.setState({ isOpen: true, photoIndex: key })} />
</div>
)
}
</React.Fragment>
}
{
isOpen && (
<Lightbox
mainSrc={images[photoIndex]}
nextSrc={images[(photoIndex + 1) % images.length]}
prevSrc={images[(photoIndex + images.length - 1) % images.length]}
onCloseRequest={() => this.setState({ isOpen: false })}
onMovePrevRequest={() =>
this.setState({
photoIndex: (photoIndex + images.length - 1) % images.length,
})
}
onMoveNextRequest={() =>
this.setState({
photoIndex: (photoIndex + 1) % images.length,
})
}
/>
)
}
</div>https://stackoverflow.com/questions/58376347
复制相似问题