0

照片对象不会通过状态在图库组件中更改。我创建了一个“文件夹库”,一个多库组件,如果您通过单击选择它来呈现一个新组件。

看演示: 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

请看下面的代码:

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}
        />
      );
    });
  }

并称之为:

{!_.isEmpty(this.state.photosObj) && this.loadGallery(columnsLength)}

参考:

4

1 回答 1

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 文件)中传递“photos={this.state.photosObj}”,<Gallery />如下所示:

代码:

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

我不确定你为什么通过其他选项。

于 2018-11-26T02:03:16.063 回答