javascript - 我无法在 React 上实现 Lightbox Gallery

标签 javascript reactjs

我遵循这个依赖的步骤: http://jossmac.github.io/react-images/

这是行不通的。没有显示图片,显示错误信息:

index.js:2178 Warning: Failed prop type: The prop onClose is marked as required in Lightbox, but its value is undefined

这是我的代码:

import React, { Component } from "react";
import Lightbox from "react-images";

const URL_INTERIORES = "http://localhost:3001/interiores";

const LIGHTBOX_IMAGE_SET = [
  {
    src: "/images/int_02.jpg",
    caption: "A forest",
    // As an array
    srcSet: ["/images/int_02.jpg", "/images/int_02.jpg"]
  },
  {
    src: "/images/int_02.jpg",
    // As a string
    srcSet: "/images/int_02.jpg 1024w, /images/int_02.jpg 800w, /images/int_02.jpg 500w, /images/int_02.jpg 320w"
  }
];

class Interiores extends Component {
  render() {
    const { open } = this.state;
    return (
      <div>
        <div>
          <Lightbox
            images={LIGHTBOX_IMAGE_SET}
            isOpen={this.state.lightboxIsOpen}
            onClickPrev={this.gotoPrevLightboxImage}
            onClickNext={this.gotoNextLightboxImage}
            onClose={this.closeLightbox}
          />
        </div>
      </div>
    );
  }
}

export default Interiores;

有人知道怎么解决吗?谢谢你

最佳答案

考虑在您的类中添加所有缺少的处理程序和状态:

class Interiores extends Component {
  state = {
     lightboxIsOpen: false
  }
  gotoPrevLightboxImage() {
     // Add the logic here
  }
  gotoNextLightboxImage() {
     // Add the logic here
  }
  closeLightbox(e) {
     // Add the logic here
  }
  render() {
    const { lightboxIsOpen } = this.state;
    return (
        <div>
          <Lightbox
            images={LIGHTBOX_IMAGE_SET}
            isOpen={lightboxIsOpen}
            onClickPrev={() => this.gotoPrevLightboxImage()}
            onClickNext={() => this.gotoNextLightboxImage()}
            onClose={e => this.closeLightbox(e)}
          />
        </div>
    );
  }
}

关于javascript - 我无法在 React 上实现 Lightbox Gallery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51663471/

相关文章:

javascript - React 中基于 api 调用返回的条件组件

javascript - 什么时候应该使用div?什么时候应该使用框架?我什么时候应该使用其他形式的动态内容?

javascript - aria-hidden=true 是否意味着您不必使用显示 :none?

javascript - Odoo:odoo TreeView 标题中的自定义按钮不会触发 python 函数

javascript - 如何在 dc.js 数据表中进行分组?

javascript - ReactJs if else 在渲染中

javascript - 重新加载页面而不影响 YouTube 进度

node.js - Apollo 查询缓存中的数据错误

javascript - React.js 从系统加载 Markdown 文件作为字符串

javascript - render 没有返回任何东西