首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将当前索引传递给react-image-lightbox?

如何将当前索引传递给react-image-lightbox?
EN

Stack Overflow用户
提问于 2019-10-14 20:01:10
回答 1查看 1K关注 0票数 0

我正在尝试实现一个灵活的显示图像的lightbox组件。我是using this npm,我从api得到的图片地址。

我应该如何将当前图像索引值传递给组件(单击了哪个图像),以便在模式上显示adject图像。

下面是我从后台得到的JSON

代码语言:javascript
复制
{ 
    "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从结果数组传递给组件,我想这可能是问题所在,有人能建议我如何将准确的索引值(准确地说,是点击的图像)传递给组件吗?

这是我的组件

代码语言:javascript
复制
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);
EN

回答 1

Stack Overflow用户

发布于 2019-10-15 16:34:01

我已经找到了一个简单的解决方案。我想这对像我这样的新手有帮助。

下面是更新组件的一部分

代码语言:javascript
复制
                <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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58376347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档