react-photo-gallery不要通过状态更改照片

时间:2018-11-25 22:00:51

标签: reactjs photo-gallery codesandbox

在图库组件中,照片对象未通过状态更改。 我创建了一个“文件夹库”,这是一个多库组件,如果您通过单击将其选中,则可以呈现一个新库。

查看演示: https://codesandbox.io/s/50wr02n8q4

github问题: https://github.com/neptunian/react-photo-gallery/issues/118

环境:

  • 反应照片库:6.2.2
  • 反应:16.6.3
  • npm:6.4.1

请参见下面的代码:

Hover A -> Focus/Enlarged A1, A2, A3
Hover B -> Focus/Enlarged B1, B2

const folderPhotos = [ { title: "Gallery one", photos: [ { src: "https://source.unsplash.com/2ShvY8Lf6l0/800x599" }, { src: "https://source.unsplash.com/Dm-qxdynoEc/800x799" }, { src: "https://source.unsplash.com/qDkso9nvCg0/600x799" } ] }, { title: "Gallery two", photos: [ { src: "https://source.unsplash.com/iecJiKe_RNg/600x799" }, { src: "https://source.unsplash.com/epcsn8Ed8kY/600x799" }, { src: "https://source.unsplash.com/NQSWvyVRIJk/800x599" } ] } ]; 维对象:

photoProps

这里的内部组件:

const photoProps = {
    width: 1,
    height: 1
  };

现在使用<Gallery columnsLength={6} photosObj={folderPhotos} photoProps={photoProps} withLightbox={true} /> 组件:

Gallery

编辑-我更新了画廊道具

我也修复了示例的import _ from "lodash"; import React, { Component, Fragment } from "react"; import Gallery from "react-photo-gallery"; import Lightbox from "react-images"; export class PhotoGallery extends Component { constructor(props) { super(props); // Bindables this.showGallery = this.showGallery.bind(this); this.openLightbox = this.openLightbox.bind(this); this.closeLightbox = this.closeLightbox.bind(this); this.goToPrevious = this.goToPrevious.bind(this); this.goToNext = this.goToNext.bind(this); } state = { photosObj: [], currentImage: 0, lightboxIsOpen: false }; async showGallery(itemObj, photoProps) { let photosObj = []; if (!_.isEmpty(itemObj)) { photosObj = await Object.keys(itemObj) .map(i => itemObj[i]) .map(item => ({ ...item, width: photoProps.width, height: photoProps.height })); this.setState({ photosObj }); console.log("-- props: ", this.state.photosObj); } } openLightbox(event, obj) { this.setState({ currentImage: obj.index, lightboxIsOpen: true }); } closeLightbox() { this.setState({ currentImage: 0, lightboxIsOpen: false }); } goToPrevious() { this.setState({ currentImage: this.state.currentImage - 1 }); } goToNext() { this.setState({ currentImage: this.state.currentImage + 1 }); } render() { const { columnsLength, photosObj, photoProps, withLightbox } = this.props; return ( <div className="section-body"> <div className="content-col-info"> <ul className="list-mentors w-list-unstyled"> {photosObj.map((itemObj, i) => ( <li key={i}> <span className="mentors-item w-inline-block" onClick={() => this.showGallery(itemObj.photos, photoProps)} > <div>{itemObj.title}</div> </span> </li> ))} </ul> </div> <div className="content-col-info"> {!_.isEmpty(this.state.photosObj) && ( <Gallery columns={columnsLength} photos={this.state.photosObj} onClick={withLightbox ? this.openLightbox : null} /> )} {!_.isEmpty(this.state.photosObj) && withLightbox && ( <Lightbox images={this.state.photosObj} onClose={this.closeLightbox} onClickPrev={this.goToPrevious} onClickNext={this.goToNext} currentImage={this.state.currentImage} isOpen={this.state.lightboxIsOpen} /> )} </div> </div> ); } } export default PhotoGallery; 组件道具。

Gallery

并称之为:

loadGallery(columnsLength) {
    import("./photo-gallery").then(Gallery => {
      console.log("-- load gallery: ", this.state.photosObj);

      return (
        <Gallery.default
          columnsLength={columnsLength}
          photosObj={this.state.photosObj}
          withLightbox={true}
        />
      );
    });
  }

参考文献:

1 个答案:

答案 0 :(得分:1)

因为在Gallery.js(库)中需要照片选项

Gallery.propTypes = {
  photos: _propTypes2.default.arrayOf(_Photo.photoPropType).isRequired,
  direction: _propTypes2.default.string,
  onClick: _propTypes2.default.func,
  columns: _propTypes2.default.number,
  margin: _propTypes2.default.number,
  ImageComponent: _propTypes2.default.func
};

在Gallery.js(您的js文件)的<Gallery />中传递“ photos = {this.state.photosObj}”,如下所示:

代码:

<Gallery
  columnsLength={columnsLength}
  photosObj={this.state.photosObj}
  photos={this.state.photosObj}
  withLightbox={true}
 />

我不确定您为什么要通过其他选择。